主模块配置:
http模块
user nginx;
worker_processes 1;#配置进程数
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
worker_rlimit_nofile 204800;
events {
use epoll;
worker_connections 20480;#允许客户端最大连接数
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
server_tokens off; #隐藏版本号
# log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
log_format access_log_json '{"user_ip":"$http_x_real_ip","lan_ip":"$remote_addr","log_time":"$time_iso8601","user_req":"$request","http_code":"$status","body_bytes_sent":"$body_bytes_sent","req_time":"$request_time","user_ua":"$http_user_agent"}';#日志以json格式输出
access_log /var/log/nginx/access.log access_log_json;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
autoindex off;
client_header_timeout 15s;
client_body_timeout 60s;
send_timeout 60s;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
include /etc/nginx/conf.d/*.conf;
}
虚拟主机配置
server { listen 80; server_name localhost; location / { proxy_pass http://172.17.0.9:3000; proxy_cache_valid 200 301 302 30m; expires 10d; include proxy.conf; } location /socket.io/ { proxy_pass http://172.17.0.9:8081/socket.io/; include proxy.conf; } location ~* ^/(video|game|music) { root /usr/share/nginx/html; include proxy.conf; proxy_cache_valid 200 301 302 30m; expires 10d; } }
proxy.conf文件配置
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_connect_timeout 60; proxy_send_timeout 60; proxy_read_timeout 60; proxy_set_header X-NginX-Proxy true; proxy_buffer_size 64k; proxy_buffers 32 32k; proxy_busy_buffers_size 128k; # nginx 1.13版本以上,支持websocket #proxy_http_version 1.1; #proxy_set_header Upgrade $http_upgrade; #proxy_set_header Connection "upgrade"; proxy_temp_file_write_size 64k; proxy_redirect off;