Nginx配置多个子配置文件以及配置文件参考范例

一台服务器上一个nginx服务器下面可能跑着很多的项目,如果把很多项目都写到同一个配置文件里 ,会导致后期难以查看与管理
因此我们需要新建一个专门放置存放子配置文件的文件夹, 然后在主配置文件nginx.conf中把这个子目录引入即可。

nginx.conf文件配置如下:

# 配置允许运行nginx服务器的用户和用户组
#user  nobody;

## 配置允许nginx进程生成的worker process数量,一般与CPU核数相等 
worker_processes  4;

## worker进程最大打开文件数,解决"too many open files"问题
#worker_rlimit_nofile 100000

## 运行错误日志存放路径
error_log logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

## pid文件存放路径和名称
pid        logs/nginx.pid;

events {
    ## 事件驱动模型,select、poll、epoll等
    use epoll;
    ## 最大连接数,直接决定并发处理能力
    worker_connections  1024;
}

## http配置
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;

    ## 加快文件读写效率
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    ## 开启gzip压缩,超过1024Kb才压缩
    gzip  on;
    gzip_min_length 1024;

    server {
        listen       8000;
        server_name  localhost;
        location / {
            root /mnt;
        }
    }

    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;
        #}
    }
    ## 在这里导入子配置文件!!!
    include test—project/*.conf;
}

这里在conf文件夹下新建了一个test-project的文件夹,然后把子配置文件xx.conf在在里面。通过在nginx.conf文件中include子配置文件即可。

test-project下的子配置文件的配置参考如下:

## 下游代理服务器,这里test-backend是自定义后台的名字,下面会用到这个。
# 这里的【127.0.0.1:9002】是后台接口地址
upstream test-backend {
    server 127.0.0.1:9002;    
}
## test-peoject虚拟服务器配置
server {
    ## 监听端口
    listen 99;
    ## 服务名称
    server_name localhost;
    ## 字符集编码
    charset utf-8;
    ## 访问日志
    access_log logs/test-peoject.access.log main;
    ## 错误日志
    #error_log logs/test-peoject.error.log main;
    # 客户端请求体最大值
    client_max_body_size 500m;
    # 黑名单配置
    deny 192.53.163.212;

    #【html/test-project】是前端项目打包好的文件的位置
    index index.html;
    root html/test-project;    

    ## 默认首页
    location / {
        try_files $uri $uri/ @router;
        index  index.html index.htm;
    }
    ## VUE路由重写
    location @router {
        rewrite ^.*$ /index.html last;
    }
    ## 显示前端静态资源
    location ~ ^(/static/) {
        access_log off;
        root html/test-project;
        expires 7d;
    }
    ## 代理前端图片,缓存时间长一点
    location ~ ^(/static/).+\.(jpg|jpeg|gif|png)$ {
        access_log off;
        root html/test-project;
        expires 15d;
    }
    ## 通过客户端请求头信息
    proxy_pass_request_headers on;
    ## 保留客户端的真实信息
    proxy_set_header Host $host;
    proxy_set_header X-Real_IP $remote_addr;
    proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
    ## 转发后台请求,这个用到了test-backend(前面已定义的)
    location /test-pro {
        proxy_pass http://test-backend;
    }
}

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值