nginx config的多个config配置

本文介绍了一种通过创建子配置文件来管理Nginx服务器上多个项目的有效方法。这种方式能够简化配置文件的管理,并提供了如何设置端口、文件路径等参数的示例。

在我们的一台服务器上,一个nginx服务器下面可能跑着许多许多的项目;

那么就需要配置多个对应的配置 端口号 已经文件入库目录等等

那么项目多了以后,把这些项目都写到一个文件里 到后期难以查看与管理

我们只需要新建一个文件夹,下面全部存放 我们的子配置 然后在主配置中把这个子目录引入即可

然后我们的主配置文件如下

user  www www;
worker_processes  8;

error_log  /www/logs/error.log info;
#access_log  /www/logs/nginx.access.log  main;
pid        /var/run/nginx.pid;


worker_rlimit_nofile 51200;

events {
    use epoll;
    worker_connections  51200;
}


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


log_format  main '"$time_local","$remote_addr","$http_x_forwarded_for","$http_host","$request","refer:$http_referer","$http_user_agent","$status","$request_time","$upstream_response_time","$body_bytes_sent","$upstream_addr","$upstream_status","$upstream_response_time","$http_cookie_pgv_pvi","$request_body","$uid_got"';


    server_names_hash_bucket_size 128;
    client_header_buffer_size 128k;
    large_client_header_buffers 4 128k;
    client_max_body_size 100m;
client_body_buffer_size 1024k;

    sendfile        on;
    tcp_nopush     on;

    keepalive_timeout  30;
    tcp_nodelay on;


  fastcgi_intercept_errors on;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 128k;
user  www www;
worker_processes  8;

error_log  /www/logs/error.log info;
#access_log  /www/logs/nginx.access.log  main;
pid        /var/run/nginx.pid;


worker_rlimit_nofile 51200;

events {
    use epoll;
    worker_connections  51200;
}


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


log_format  main '"$time_local","$remote_addr","$http_x_forwarded_for","$http_host","$request","refer:$http_referer","$http_user_ag
ent","$status","$request_time","$upstream_response_time","$body_bytes_sent","$upstream_addr","$upstream_status","$upstream_response
_time","$http_cookie_pgv_pvi","$request_body","$uid_got"';


    server_names_hash_bucket_size 128;
    client_header_buffer_size 128k;
    large_client_header_buffers 4 128k;
    client_max_body_size 100m;
client_body_buffer_size 1024k;

    sendfile        on;
    tcp_nopush     on;

    keepalive_timeout  30;
    tcp_nodelay on;


  fastcgi_intercept_errors on;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 128k;
  fastcgi_buffers 4 128k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;

    gzip  on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types       text/plain application/x-javascript text/css application/xml image/jpeg image/gif image/png;
    gzip_vary on;

    #server {
#       listen 80;
#       server_name  localhost;
 #       location / {
#           proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;
 #           proxy_pass http://78list.cn;
            #proxy_set_header Host www.yourdomain.com;
  #          proxy_set_header X-Forwarded-For $remote_addr;
   #     }

    #}
    #upstream 78list.cn {
    #    server 192.168.8.113:8080;
    #}  

    include /etc/nginx/default.conf;
    include /etc/nginx/upstream/*.conf;
    include /etc/nginx/conf.d/*.conf; // 这里就是引入的子配置文件夹
}

一个范例 子配置的conf

server
  {
    listen       832; // 端口号
    server_name localhost; // 域名
    index index.html index.htm index.php;
    root  /home/www/ai/crm/web/public; //项目的入口文件夹

        location ~ /.svn/ {
        deny all;
    }


    location / {
        rewrite ^/$ /index.php last;
        rewrite ^/(?!index\.php|index\.html|layui|css|js|bootstrap|robots\.txt)(.*)$ /index.php/$1 last;
    }

   location ~ \.php {
                fastcgi_pass 127.0.0.1:9002;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }


    location ~/uploads/.*\.(php|php5)?$ {
        deny all;
    }
    location ~/public/.*\.(php|php5)?$ {
        deny all;
    }

   location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
      expires      30d;
    }

    location ~ .*\.(js|css)?$
   {
      expires      8d;
    }


    #access_log  /www/logs/access.log  main;

  }

 

转载于:https://www.cnblogs.com/djwhome/p/9346218.html

### Nginx 常规配置示例 #### 配置文件结构概述 Nginx配置文件通常由多个部分组成,这些部分定义了服务器的行为。整个配置文件可以分为几个主要区块: - **全局块**:设置影响 Nginx 整体运行的参数。 - **events 块**:优化连接处理选项。 - **http 块**:这是最核心的部分,包含了 HTTP 协议相关的设定以及虚拟主机(`server`)的定义。 ```nginx # 全局块 user nobody; worker_processes auto; error_log logs/error.log warn; pid logs/nginx.pid; ``` #### Events 块配置 Events 块主要用于调整异步事件处理器的相关参数,这对于提高性能至关重要。 ```nginx events { worker_connections 1024; # 设置单个工作进程的最大并发连接数 } ``` #### Http 块及其子元素 Http 块内包含了许重要的指令和嵌套的 `server` 及 `location` 子块来指定如何响应客户端请求。 ##### Server 块配置 Server 块用于定义一个或多个监听端口上的站点行为,并可包含多个 Location 块以进一步细化路径匹配规则。 ```nginx http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; charset utf-8; # 访问日志格式化 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; root html; index index.html index.htm; location / { try_files $uri $uri/ =404; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } ``` 此段代码展示了基本的日志记录方式、根目录位置(`root`)、默认首页文件(`index`)的选择以及其他一些常见的配置项[^2]。 #### Gzip压缩支持 为了加速页面加载速度并减少带宽消耗,在生产环境中启用Gzip是非常推荐的做法。 ```nginx gzip on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ``` 上述配置开启了对种MIME类型的动态压缩功能[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值