Nginx在Ubuntu上的基本知识

demo


1.在线安装

$sudo apt-get install nginx

ubuntu安装Nginx之后的文件结构如下:

所有的配置文件都在/etc/nginx下,并且每个虚拟主机已经安排在了/etc/nginx/sites-available下

启动程序文件在/usr/sbin/nginx

日志放在了/var/log/nginx中,分别是access.log和error.log

并已经在/etc/init.d/下创建了启动脚本nginx

默认的虚拟主机的目录设置在了/usr/share/nginx/www

2.源代码安装

下载地址:http://nginx.org/download/

我这里下载的是 nginx-1.3.9.tar.gz,安装过程很简单,如下:

$./configure

$make

$make install

安装成功之后,nginx放置在/usr/local/nginx目录下,主要的配置文件为conf目录下的nginx.conf,nginx的启动文件在sbin目录下的nginx文件。

3.修改监听端口

修改nginx的配置文件nginx.conf,将一下这一行 listen 80; 修改为 listen 8080; 然后就可以访问了,http://localhost:8080/

4. 配置文件

Nginx的配置文件是/etc/nginx/nginx.conf,其中设置了一些必要的参数,我们发现其中这样的语句:
include /etc/nginx/sites-enabled/* 可以看出 /etc/nginx/sites-enabled/default 文件也是一个核心的配置文件,其中包含了主要的配置信息,如服务器跟目录、服务器名称、location信息和server信息。

对于源代码安装的nginx,配置文件为 /usr/local/nginx/conf/nginx.conf

5. location的匹配规则

(1)= 前缀的指令严格匹配这个查询。如果找到,停止搜索。

(2)剩下的常规字符串,最长的匹配优先使用。如果这个匹配使用 ^~ 前缀,搜索停止。

(3)正则表达式,按配置文件里的顺序,第一个匹配的被使用。

(4)如果第三步产生匹配,则使用这个结果。否则使用第二步的匹配结果。

在location中可以使用常规字符串和正则表达式。如果使用正则表达式,你必须使用以下规则:

  • ~* 前缀选择不区分大小写的匹配
  • ~ 选择区分大小写的匹配
1
2
3
4
location = / {
# 只匹配 / 查询。
[ configuration A ]
}
1
2
3
4
5
location / {
# 匹配任何查询,因为所有请求都以 / 开头。
# 但是正则表达式规则和长的块规则将被优先和查询匹配。
[ configuration B ]
}
1
2
3
4
5
location ^~ /images/ {
# 匹配任何以 /images/ 开头的任何查询并且停止搜索。
# 任何正则表达式将不会被测试。
[ configuration C ]
}
1
2
3
4
5
location ~* \.(gif|jpg|jpeg)$ {
# 匹配任何以 gif、jpg 或 jpeg 结尾的请求。
# 然而所有 /images/ 目录的请求将使用 Configuration C。
[ configuration D ]
}

6. 常用命令

-?,-h : this help

-v : show version and exit

-V : show version and configure options then exit

-t : test configuration and exit

-q : suppress non-error messages during configuration testing

-s signal : send signal to a master process: stop, quit, reopen, reload

-p prefix : set prefix path (default: /usr/local/nginx/)

-c filename : set configuration file (default: conf/nginx.conf)

-g directives : set global directives out of configuration file

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. 1.在线安装
  2. 2. 2.源代码安装
  3. 3. 3.修改监听端口
  4. 4. 4. 配置文件
  5. 5. 5. location的匹配规则
  6. 6. 6. 常用命令
,