为了方便nginx维护,一般反向代理通过分配置文件管理,所以用到include指令
简化前的配置 nginx.conf
user nobody nobody;
worker_processes 4;
pid logs/nginx.pid;
error_log logs/error.log error;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
include proxy.conf;
sendfile on;
tcp_nopush on;
tcp_nodelay off;
keepalive_timeout 0;
default_type application/octet-stream;
upstream backend {
server 192.168.1.188:8080 srun_id=c1 weight=1;
jvm_route $cookie_JSESSIONID reverse;
}
server {
listen 80;
server_name foo.com;
index index.html index.htm;
access_log logs/access.log;
location / {
proxy_pass http://backend;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 50M;
client_body_buffer_size 256k;
proxy_connect_timeout 600;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /status {
stub_status on;
access_log off;
allow all;
deny all;
}
}
}
使用 include 指令简化配置文件
抽取 proxy 设置到单独文件中(conf/proxy.conf)
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 50M;
client_body_buffer_size 256k;
proxy_connect_timeout 600;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
抽取 status 设置到单独文件中(conf/status.conf)
location /status {
stub_status on;
access_log off;
allow all;
deny all;
}
抽取杂项配置到单独文件中(conf/misc.conf)
sendfile on;
tcp_nopush on;
tcp_nodelay off;
keepalive_timeout 0;
default_type application/octet-stream;
新增存放反向代理目录,每一个反向代理都分配置文件
- 新增目录目录
- 新增反向代理配置,如后台的反向代理
server {
listen 80;
server_name foo.com;
charset utf-8;
index index.html index.htm;
access_log logs/access.log;
location / {
proxy_pass http://backend;
}
include status.conf;
}
最终简化后的配置
user nobody nobody;
worker_processes 4;
pid logs/nginx.pid;
error_log logs/error.log error;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
include proxy.conf;
incluee misc.conf;
upstream backend {
server 192.168.1.188:8080 srun_id=c1 weight=1;
jvm_route $cookie_JSESSIONID reverse;
}
# 配置目录下全部引入
include /etc/nginx/conf.d/*.conf;
}