nginx配置文件分开配置

Posted 潇潇六月雨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx配置文件分开配置相关的知识,希望对你有一定的参考价值。

在Linux中不同的用户都可能用到nginx,如果不同的用户无法达成一个对nginx.conf编写标准,势必会导致nginx.conf里的内容变的相当混乱,极难维护。所以这里建议新建一个文件夹,这个文件夹中分放不同用户所需要反向代理的配置文件。

nginx.conf 文件尽量不做修改,只需在最末尾加上  include conf.d/*.conf;

nginx.conf

技术图片
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    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;
    
    # upload file max size
    client_max_body_size 300m;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the php scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache‘s document root
        # concurs with nginx‘s one
        #
        #location ~ /.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    
    
    # 各个域名单独控制
    include conf.d/*.conf;
    
}
技术图片

 

在conf.d文件中放入不同用户的conf文件。

如: zhangsan.conf   lisi.conf

zhangsan.conf

技术图片
server {
    listen 80;
    server_name    zhangsan.xxx.com;
    location / {
        proxy_set_header    Host            $host:80;
        proxy_set_header    X-Real-IP       $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_hide_header   X-Powered-By;
        proxy_pass http://127.0.0.1:8080;    
    }
}
技术图片

 

以上是关于nginx配置文件分开配置的主要内容,如果未能解决你的问题,请参考以下文章

nginx.conf 忽略了 nginx-ingress 配置映射片段

Nginx 配置日志以天为单位分开保存

Nginx常用功能配置实战

nginx 配置说明

nginx基础优化

[Nginx]nginx 配置实例-动静分离