nginx一个配置文件分割成多个配置文件(实际还是一个)

前言:因为一个服务器下的服务越来越多,都需要nginx做代理,那都写到同一个配置文件下比较麻烦了,可编辑,可读性比较差,那怎处理呢,可以把配置文件做分割。修改或新增一个服务代理,而不会影响到别的服务代理。

1.修改nginx.conf配置文件

   原server 全部注释掉,新的server 在所引用的配置文件中
   此处表示引用 /etc/nginx/conf.d/下的所配置文件
    include /etc/nginx/conf.d/*.conf;

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    #此处表示引用 /etc/nginx/conf.d/下的所配置文件
    include /etc/nginx/conf.d/*.conf;

#原server 全部注释掉,新的server 在所引用的配置文件中

#    server {
#        listen       80;
#        listen       [::]:80;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#        location = /404.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#        location = /50x.html {
#        }
#    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

 引用的配置文件

nginx一个配置文件分割成多个配置文件(实际还是一个)_第1张图片

2.把其中一个服务代理比如mycrm写成mycrm.conf 

#被代理对象tomcat端口8004 
#crmmycomm  为新起的被代理对象的名字,下面proxy_pass会被引用到
upstream crmmycomm  {
       server 123.57.123.31:8004;
}


## Start www.nowamagic.net ##
server {
    listen   80;#代理端口80

    server_name config.mycomm.cn;#域名
    client_header_buffer_size 512k;
    large_client_header_buffers 4 512k;


    ssl_certificate  /etc/nginx/cert/server.crt;
    ssl_certificate_key  /etc/nginx/cert/server.key;
    ssl_session_timeout 5m;


    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;



    #access_log  /var/log/nginx/jx.access.log  main;
    #error_log  /var/log/nginx/jx.error.log;
    #root   sdsxsw;
    #index  index.html index.htm index.php;

    ## send request back to apache ##

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #config 访问的路径
    location /config {
    	proxy_pass   http://crmmycomm/config/;
        client_max_body_size    100m;
        #Proxy Settings
    	proxy_redirect     default;
    	proxy_set_header   Host             $host:$server_port;
    	proxy_set_header   X-Real-IP        $remote_addr;
    	proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    	proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
    	proxy_max_temp_file_size 0;
    	proxy_connect_timeout      90;
    	proxy_send_timeout         90;
    	proxy_read_timeout         90;
    	proxy_buffer_size          4k;
    	proxy_buffers              4 32k;
    	proxy_busy_buffers_size    64k;
    	proxy_temp_file_write_size 64k;
   }
}

3.新增其它服务代理同2步骤

最后看结果mycrm对代理成功

nginx一个配置文件分割成多个配置文件(实际还是一个)_第2张图片

若新增配置文件或修改配置文件使用刷新配置文件命令:

nginx -s reload
就可以了,也不用重启nginx

你可能感兴趣的:(nginx,大数据)