同时解开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;
配置文件加载顺序: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 / {# “/” 拦截所有请求
proxy_pass http://127.0.0.1:8080/; # 所有到localhost:8888端口请求反向代理转向目标地址
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;
}
}
创建新文件 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 / {# “/” 拦截所有请求
proxy_pass http://127.0.0.1:8181/;# 所有到localhost:9999端口请求反向代理转向目标地址
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 {
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;
}
}
原文地址:http://blog.51cto.com/damon188/2073724