開啓 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;
}
}