续:nginx安装配置


开启 nginx 请求日志

同时解开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  logs/access.log  main;

1.12.1版本需要手动在 nginx/1.12.1/目录下新建logs目录。启动可以自动生成access.log文件。

另外可以针对具体 server 生成日志文件,server 模块中加入如下配置:

error_log    logs/error_8888.log    error;
access_log    logs/access_8888.log    main;
基于 server 模块反向代理配置

配置文件加载顺序:nginx.conf --> conf.d/*.conf
文件 conf.d/*.conf 中内容是 server 模块
修改配置文件 conf.d/XXX.conf

# server 1
    server {
        listen       8888;# 监听8888端口
        server_name  localhost;# 监听服务器
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        error_log    logs/error_8888.log    error;
        access_log    logs/access_8888.log    main;
        location / {# “/” 拦截所有http://localhost:8888/请求
            proxy_pass http://127.0.0.1:8080/; # 所有到http://localhost:8888/请求反向代理转向目标地址http://127.0.0.1:8080/
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Firwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP $remote_addr;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

服务路径携带别名

# server 1
    server {
        listen       8888;# 监听8888端口
        server_name  localhost;# 监听服务器
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        error_log    logs/error_8888.log    error;
        access_log    logs/access_8888.log    main;
        location /guaguale/ {# 拦截所有http://localhost:8888/guaguale/请求
            proxy_pass http://127.0.0.1:8080/guaguale/; # 所有到http://localhost:8888/guaguale/请求反向代理转向目标地址http://127.0.0.1:8080/guaguale/
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Firwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP $remote_addr;
        }

                # 请求异常500 502 503 504 转向地址
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html; # 设置资源根路径
        }

    }
基于 server 模块反向代理多服务配置

创建新文件 conf.d/YYY.conf,新建 server 模块,具体配置类似上面server。

# server 2
    server {
        listen       9999;# 监听9999端口
        server_name  localhost;# 监听服务器
        error_log    logs/error_9999.log    error;
        access_log    logs/access_9999.log    main;
        location /heimdall/ {# 拦截所有http://localhost:9999/heimdall/请求
            proxy_pass http://127.0.0.1:8181/heimdall/;# 所有到http://localhost:9999/heimdall/请求反向代理转向目标地址http://127.0.0.1:8181/heimdall/
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Firwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP $remote_addr; 
        }

    }
基于 server 模块静态资源分离配置

同样道理可以做静态资源分离。

server {
        listen       80;
        server_name  localhost;

        # static file
        location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|js|css)$ { # 正则校验过滤
              root /usr/share/nginx/html; # 设置静态资源根路径地址
              #cache 缓存失效时间
              expires 3d;
        }
}