使用 include 指令简化 Nginx 配置

为了方便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;

新增存放反向代理目录,每一个反向代理都分配置文件

  1. 新增目录 c o n f / c o n f . d \color{red}{conf/conf.d} conf/conf.d目录
  2. 新增反向代理配置,如后台的反向代理 c o n f / c o n f . d / b a c k e n d . c o n f \color{red}{conf/conf.d/backend.conf} conf/conf.d/backend.conf
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;
}
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值