nginx.config详解

《------------------------------------全局块------------------------------》

#user  nginx;
#nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数。
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

《------------------------------------全局块------------------------------》

 从文件开始到events块之间的内容,主要是设置nginx服务器整体配置,比如服务器用户,进程存放,日志文件存放定义,文件引入等

《——————————————————events块————————————》

events {
    #单个后台worker process进程的最大并发链接数  
    worker_connections  2018;
}

《——————————————————events块————————————》

《——————————————————http 块————————————》

http {
《——————————————————http全局 块————————————》
    #设定mime类型,类型由mime.type文件定义
    include       /etc/nginx/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  /var/log/nginx/access.log  main;
    
    #开启gzip压缩
    gzip  on;
    gzip_disable "MSIE [1-6].";
    
    #设定请求缓冲
    client_header_buffer_size    128k;
    large_client_header_buffers  4 128k;
    
    #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,
    #对于普通应用,必须设为 on,
    #如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,
    #以平衡磁盘与网络I/O处理速度,降低系统的uptime.
    sendfile on;
    
    #用于设置客户端连接保持活动的超时时间,在超过这个时间之后服务器会关闭该链接。
    #keepalive_timeout  0;
    keepalive_timeout  120; 
    
    #允许客户端请求的最大单文件字节数
    client_max_body_size 50m;
    
    #服务器名字的hash表大小
    server_names_hash_bucket_size 128;
    #header中自定

义变量时支持下划线
    underscores_in_headers on; 
   《——————————————————http全局 块————————————》 

   《——————————————————server全局 块————————————》 

    server {
        listen 8002;                               //server全局快配置监听和服务
        server_name  localhost;

#location用来匹配同一域名下多个URI的访问规则
#比如动态资源如何跳转,静态资源如何跳转等
#location后面跟着的/代表匹配规则
        location / {                                        //localtion块  配置自己的代理

 #站点根目录,可以是相对路径,也可以使绝对路径
            root   html;
            #默认主页
           index  index.html index.htm;
          
           #转发后端站点地址,一般用于做软负载,轮询后端服务器
           #proxy_pass http://10.11.12.237:8080;
 
            #拒绝请求,返回403,一般用于某些目录禁止访问
            #deny all;
            
           #允许请求
           #allow all;
             
            add_header 'Access-Control-Allow-Origin' '*';
          add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
             #重新定义或者添加发往后端服务器的请求头
            #给请求头中添加客户请求主机名
            proxy_set_header Host $host;
           #给请求头中添加客户端IP
             proxy_set_header X-Real-IP $remote_addr;
           #将$remote_addr变量值添加在客户端“X-Forwarded-For”请求头的后面,并以逗号分隔。 如果客户端请求未携带“X-Forwarded-For”请求头,$proxy_add_x_forwarded_for变量值将与$remote_addr变量相同  
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #给请求头中添加客户端的Cookie
            proxy_set_header Cookie $http_cookie;
             #将使用代理服务器的主域名和端口号来替换。如果端口是80,可以不加。
             proxy_redirect off;
             
             #浏览器对 Cookie 有很多限制,如果 Cookie 的 Domain 部分与当前页面的 Domain 不匹配就无法写入。
            #所以如果请求 A 域名,服务器 proxy_pass 到 B 域名,然后 B 服务器输出 Domian=B 的 Cookie,
         #前端的页面依然停留在 A 域名上,于是浏览器就无法将 Cookie 写入。
            
          #不仅是域名,浏览器对 Path 也有限制。我们经常会 proxy_pass 到目标服务器的某个 Path 下,
            #不把这个 Path 暴露给浏览器。这时候如果目标服务器的 Cookie 写死了 Path 也会出现 Cookie 无法写入的问题。
            
            #设置“Set-Cookie”响应头中的domain属性的替换文本,其值可以为一个字符串、正则表达式的模式或一个引用的变量
            #转发后端服务器如果需要Cookie则需要将cookie domain也进行转换,否则前端域名与后端域名不一致cookie就会无法存取
        #配置规则:proxy_cookie_domain serverDomain(后端服务器域) nginxDomain(nginx服务器域)
            proxy_cookie_domain localhost .testcaigou800.com;
             
             #取消当前配置级别的所有proxy_cookie_domain指令
             #proxy_cookie_domain off;
            #与后端服务器建立连接的超时时间。一般不可能大于75秒;
            proxy_connect_timeout 30;
        }
        location /apis {
            rewrite ^/apis/?(.*)$ /$1 break;
            include uwsgi_params;
            proxy_pass http://192.168.0.202:9099;
        }
《——————————————————server全局 块————————————》 
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;                        
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        add_header Access-Control-Allow-Origin *;
    }
    《——————————————————http 块————————————》
}
————————————————
版权声明:本文为CSDN博主「caoyanzhi」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/caoyanzhi/article/details/107317912

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值