Nginx 配置文件

参考资料1
参考资料2
参考资料3

主配置文件 /etc/nginx/nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;

    # 必须加上,否则有可能乱码
    charset utf-8;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    server {
        listen 80;
        server_name lishiqing.com www.lishiqing.com;
        index index.php index.html;
        root /data/web/lishiqing;

        # 匹配以 .php 结尾的请求,转义字符必须加上,所有 .php 文件转交给 fastcgi 来处理
        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
        }

        # 可以设置静态文件的过期时间,没过期之前,nginx 不会重复返回对应的静态资源
        # 所有静态文件由 nginx 直接读取,不用交给 fastcgi,从 $root/$url
        location ~ \.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|
        pdf|xls|mp3|wma)$ {
            expires 15d; 
        }

        location ~ \.(js|css) {
            expires 1h;
        }

        # 找不到请求的资源,返回 $root/404.html 页面,这里是 /data/web/lishiqing/404.html
        error_page 404 /404.html;  
        # 服务器发生错误,返回 $root/50x.html 页面,但是经测试不返回该页面,直接报告错误。
        error_page 500 502 503 504 /50x.html;
        # 经测试加不加这个选项都没用,服务器脚本发生错误,不会返回 $root/50x.html 页面,直接报告错误
        # proxy_intercept_errors on;
    }
}

配置文件中的 path,既可以是绝对路径也可以是相对路径,相对路径相对 /usr/share/nginx 目录。

server

server {
    listen 80;
    server_name lishiqing.com www.lishiqing.com;
    # 相对路径,相对于 /usr/share/nginx 目录
    # access_log logs/access.log main;
    # error_log  logs/error.log error;
    # 绝对路径
    access_log /usr/share/nginx/logs/access.log main;
    error_log  /usr/share/nginx/logs/error.log error;

    # 相对路径,相对于 /usr/share/nginx 目录
    # root html;
    # 绝对路径
    root /usr/share/nginx/html;

    # 相对路径或者绝对路径都一样,都是相对于 root 的路径,寻找 /usr/share/nginx/html/404.html
    # 相对路径,如果找不到资源,返回 404.html 页面,浏览器 url 跳转到 /404.html
    # error_page 404 404.html;
    # error_page 500 502 503 504 50x.html;
    # 绝对路径,如果找不到资源,返回 404.html 页面,浏览器 url 不变
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
}

listen

  • 格式:listen IP:PORT;
  • 如果不指定 IP,则默认 *
  • 如果不指定 PORT,则默认 80
  • 如果某个请求匹配 listen 的 IP:PORT,则应用该 server 服务器
# 标准格式
listen 192.168.100.104:80;
# 以下三个相同
listen 80;
listen *:80;
listen 0.0.0.0:80;
# 以下两个相同
listen 192.168.100.104;
listen 192.168.100.104:80;

通过 default_server 参数,指定当前 server 为对应 IP:PORT 对的默认服务器。当一个请求匹配到 IP:PORT,但是 Host 字段不匹配任何一个 server_name,则应用默认服务器 server。假设 http://www.lishiqing.com 请求的 IP:PORT 为 192.168.100.104:80,但是没有对应的 server_name 匹配 www.lishiqing.com,则应用默认服务器。

server {
    listen 192.168.100.104:80;
    server_name lishiqing.com;
}

# 192.168.100.104:80 的默认服务器
server {
    listen 192.168.100.104:80 default_server;
    server_name zhaojinmai.com;
}

如果没有一个 listen 指令具有 default_server 参数,则具有 IP:PORT 对的第一个 server 将是该 IP:PORT 对的默认服务器。

# 192.168.100.105:80 的默认服务器
server {
    listen 192.168.100.105:80;
    server_name linmiaomiao.com;
}

# 192.168.100.104:80 的默认服务器
server {
    listen 192.168.100.104:80;
    server_name lishiqing.com;
}

server {
    listen 192.168.100.104:80;
    server_name zhaojinmai.com;
}

server_name

  • 格式:server_name DOMAIN;
  • 如果相同的 IP 和 PORT 只有一个 server 块,会忽略 server_name
  • 如果相同的 IP 和 PORT 有多个 server 块,会根据 server_name 区分应用哪个 server
  • server_name 根据请求的 Host 字段来查看是否匹配,一般填写域名
# 标准格式
server_name lishiqing.com;
# 多个 domain 用空格分隔
server_name lishiqing.com www.lishiqing.com;

以下配置忽略 server_name,所有指向 192.168.100.105 的域名都能应用该 server

server {
    listen 192.168.100.105:80;
    server_name linmiaomiao.com;
}

以下配置访问 lishiqing.com 应用默认 server,也就是第一个 server

server {
    listen 192.168.100.105:80;
    server_name linmiaomiao.com;
}

server {
    listen 192.168.100.105:80;
    server_name zhaojinmai.com;
}

location

参考资料

location 指令可以跟字符串,也可以跟正则表达式

location string_path {} 区分大小写的字符串
location = string_path {} 严格匹配字符串
location ~ reg_path {} 区分大小写的正则表达式
location ~* reg_path {} 不区分大小写的正则表达式

http://lishiqing.com/shop/images/1.png 这个请求的 query 为 /shop/images/1.png

当 location 后面是字符串的时候,如果某个请求的 query 以该字符串开头,则匹配该 location,比如 /shop/images/1.png 的请求匹配 location /shop {},也匹配 location / {},但是不匹配 location /blog

当 location 后面是正则表达式的时候,如果某个请求的 query 匹配该正则表达式,则匹配该 location,比如 /shop/images/1.png 的请求匹配 location ~ \.(jpg|jpeg|png)$ {}

如果某个 query 匹配多个字符串的 location,那么应用匹配度最高的一个 location,与 location 的顺序无关。比如 /shop/images/1.png 的请求匹配 location / {}location /shop {},但是应用 location /shop {}

如果某个 query 匹配多个正则表达式的 location,那么按照 location 的顺序应用第一个匹配的 location。比如 /shop/images/1.png 的请求匹配 location ~ \.(jpg|jpeg|png)$ {}location ~ .*/images/.* {},但是应用第一个匹配的 location ~ \.(jpg|jpeg|png)$ {}。因为在 query 在检查正则表达式的时候,遇见第一匹配的 location 就停止继续查找了。

如果某个 query 既匹配某个字符串的 location,又匹配某个正则表达式的 location,那么应用正则表达式的 location,与顺序无关。比如 /shop/images/1.png 的请求匹配 location /shop {}location ~ \.(jpg|jpeg|png)$ {},但是会应用 location ~ \.(jpg|jpeg|png)$ {}。这是因为某个请求会先检查跟着字符串的 location,不管是否找到匹配的字符串 location,都会继续按照顺序查找跟着正则表达式 location,因此正则表达式的优先级高于字符串。如果找到了匹配的正则表达式,则立即停止查找,应用该 location,如果没找到匹配的正则表达式,则应用刚才找到的匹配度最高的字符串 location。

location = string_path {} 为严格匹配,query 必须与 string_path 完全一致才能匹配。如果 query 与某个 = string_path 匹配,则立即停止查找,应用该 location。意味着不会去查找正则表达式 location。比如 /shop/images/1.png 匹配 location = /shop/images/1.png {},不匹配 location = /shop {}location = /shop/images。一般用来设置 location = / {} 来匹配 / 的 query,因为 / 的 query 会很频繁,因此将 location = / {} 放在第一条 location 可以提高匹配查询的效率。

location ^~ string_path {} 这种字符串匹配的优先级高于正则表达式,当某个 query 匹配该 location 的时候,不会继续查找正则表达式的 location 了。比如 /shop/images/1.png 的 query 满足 location ^~ /shop {}location ~ \.(jpg|jpeg|png)$ {},但是会应用 location ^~ /shop {}

总结:

  • 先查找字符串 location,再查找正则表达式 location
  • query 匹配多个字符串 location,则应用匹配度最高的,与顺序无关
  • query 匹配多个正则表达式 location,则应用第一个匹配的,与顺序有关
  • query 既匹配字符串 location,又匹配正则表达式 location,则应用正则表达式 location,与顺序无关
  • query 严格匹配某个 = 字符串 的 location,则立即应用该 location,停止继续查找
  • query 匹配某个 ^~ 字符串的 location,则立即应用该 location,停止继续查找正则表达式 location(?存在疑问,是否会继续查找剩余的字符串 location)

你可能感兴趣的:(Nginx 配置文件)