nginx配置样例

5 篇文章 0 订阅
#user  nobody;
#nginx线程数量
worker_processes  8;

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

pid        logs/nginx.pid;


events {
    #nginx最大连接数
    worker_connections 65535;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    #nginx日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" "$resp_body"';
    #关闭access_log
    #access_log off;
    access_log  logs/access.log main;
    sendfile        on;
    #tcp_nopush     on;

    #保持连接时间;
    keepalive_timeout  65;
  
    #php服务
    server {
        listen          80;
        server_name     10.130.161.19;
        index           index.html index.htm index.php;
        root            /opt/local/phpServer;
        access_log      off;

        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;

        location ~ \.php$ {
            fastcgi_pass    unix:/tmp/php-cgi.sock;
            fastcgi_index   index.php;
            include         fcgi.conf;
        }
    }

    #配置upstream
    upstream  httptest {
        server   127.0.0.1:9011 weight=1;
        server   127.0.0.1:9012 weight=1;
    }
    upstream  websocketTest {
        server   127.0.0.1:9021 weight=1;
    }
    
    server {
        listen  80;
        #配置域名
        server_name  test.com;

        #此为配置websocket即ws://的方式
        location ^~ /websocket-test/ {
            proxy_pass         http://websocketTest;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }

        #此为配置普通http的方式
        location ^~ /http-test/ {
            #以下三行为跨域配置项,当前端使用nodejs时会用到,也可直接配在server下面
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
            add_header Access-Control-Allow-Headers 'X-Content-Sender,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';

            proxy_pass         http://httptest;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
      }
      
      #https配置方式
      server {
        listen 443 ssl;
        server_name 10.130.161.19;
        ssl on;
        ssl_certificate /opt/local/nginx/ssl/nginx42.crt;
        ssl_certificate_key /opt/local/nginx/ssl/nginx42.key;
        ssl_session_cache shared:SSL:10m;#此处为缓存ssl证书容量,据官方文档所述,cache中的1m可以存放4000个session
        ssl_session_timeout 10m;
        set $resp_body "";

        location ^~ /https-test/ {
          proxy_pass         http://httptest;
          proxy_set_header   Host             $host;
          proxy_set_header   X-Real-IP        $remote_addr;
          proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
          #此处为打印返回值,跟上面的 set $resp_body一起配置
          #lua_need_request_body on;
          #  body_filter_by_lua '
          #      local resp_body = string.sub(ngx.arg[1], 1, 1000)
          #      ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
          #      if ngx.arg[2] then
          #          ngx.var.resp_body = ngx.ctx.buffered
          #      end
          #  ';
        }

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值