安全问题,建议用nobody,不要用root. user root; worker数和服务器的cpu数相等是最为适宜 worker_processes 1; error_log path(存放路径) level(日志等级)path表示日志路径,level表示日志等级, 具体以下:[ debug | info | notice | warn | error | crit ] 从左至右,日志详细程度逐级递减,即debug最详细,crit最少,默认为crit。 error_log logs/error.log; error_log logs/error.log notice; error_log logs/error.log info; pid logs/nginx.pid; events { #这个值是表示每一个worker进程所能创建链接的最大值,因此,一个nginx能创建的最大链接数, #应该是worker\_connections * worker\_processes。 #固然,这里说的是最大链接数,对于HTTP请求本地资源来讲,可以支持的最大并发数量是worker\_connections * worker\_processes, #若是是支持http1.1的浏览器每次访问要占两个链接, #因此普通的静态访问最大并发数是: worker\_connections * worker\_processes /2, #而若是是HTTP做为反向代理来讲,最大并发数量应该是worker\_connections * worker\_processes/4。 #由于做为反向代理服务器,每一个并发会创建与客户端的链接和与后端服务的链接,会占用两个链接。 worker_connections 1024; #这个值是表示nginx要支持哪一种多路io复用。 #通常的Linux选择epoll, 若是是(*BSD)系列的Linux使用kquene。 #windows版本的nginx不支持多路IO复用,这个值不用配。 use epoll; # 当一个worker抢占到一个连接时,是否尽量的让其得到更多的链接,默认是off 。 multi_accept on; # 默认是on ,开启nginx的抢占锁机制。 accept_mutex on; } tcp/udp 代理 stream { upstream backend { # 一、轮询(默认) # 每一个请求按时间顺序逐一分配到不一样的后端服务器,若是后端服务器down掉,能自动剔除。 # 二、指定权重 # 指定轮询概率,weight和访问比率成正比,用于后端服务器性能不均的状况。 #三、IP绑定 ip_hash # 每一个请求按访问ip的hash结果分配,这样每一个访客固定访问一个后端服务器,能够解决session的问题。 #四、备机方式 backup # 正常状况不访问设定为backup的备机,只有当全部非备机全都宕机的状况下,服务才会进备机。 #五、fair(第三方) #按后端服务器的响应时间来分配请求,响应时间短的优先分配。 #六、url_hash(第三方) #按访问url的hash结果来分配请求,使每一个url定向到同一个后端服务器,后端服务器为缓存时比较有效。 #还能够设置请求失败的次数。max_fails #hash $remote_addr consistent; # 代理tcp,都代理到3306端口,可是weight(权重)不同,代理到252服务的几率为50%,代理到251服务的几率为30%. server 192.168.1.252:3306 weight=5 max\_fails=3 fail\_timeout=30s; server 192.168.1.253:3306 weight=2 max\_fails=3 fail\_timeout=30s; } #配置tcp server { listen 3306; proxy_connect_timeout 3s; proxytimeout 10s; proxypass backend; } upstream dnsservers{ server 192.168.47.44:53; } #配置udp server { listen 53 udp; #容许访问的IP allow 192.168.21.216; #除216以外的IP都不容许访问 deny all; #UDP traffic will be proxied to the "dnsservers" upstream group proxy_pass dns_servers; } } http { # lua 脚本的配置 lua\_package\_path "/usr/local/src/nginx/conf/waf/?.lua"; lua\_shared\_dict limit 10m; init\_by\_lua_file /usr/local/src/nginx/conf/waf/init.lua; access\_by\_lua_file /usr/local/src/nginx/conf/waf/waf.lua; #当web服务器收到静态的资源文件请求时,依据请求文件的后缀名在服务器的MIME配置文件中找到对应的MIME Type,再根据MIME Type设置HTTP Response的Content-Type,而后浏览器根据Content-Type的值处理文件。 include mime.types; #若是 不能从mime.types找到映射的话,用如下做为默认值 default_type application/octet-stream; #关闭反向代理缓存 proxy_buffering off; #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; #开启从磁盘直接到网络的文件传输,适用于有大文件上传下载的状况,提升IO效率。 sendfile on; #tcp_nopush on; #一个请求完成以后还要保持链接多久, 默认为0,表示完成请求后直接关闭链接。 #keepalive_timeout 0; keepalive_timeout 65; #开启或者关闭gzip模块 #gzip on; #设置容许压缩的页面最小字节数,页面字节数从header头中的Content-Length中进行获取。 #gzip_min_lenth 1k; # gzip压缩比,1 压缩比最小处理速度最快,9 压缩比最大但处理最慢(传输快但比较消耗cpu) #gzip\_comp\_level 4; #匹配MIME类型进行压缩,(不管是否指定)"text/html"类型老是会被压缩的。 #gzip_types types text/plain text/css application/json application/x-javascript text/xml #动静分离 #服务器端静态资源缓存,最大缓存到内存中的文件,不活跃期限 open\_file\_cache max=655350 inactive=20s; #活跃期限内最少使用的次数,不然视为不活跃。 open\_file\_cache\_min\_uses 2; #验证缓存是否活跃的时间间隔 open\_file\_cache_valid 30s; server { listen 81; server_name nginx.test1.com; #charset koi8-r; #access_log logs/host.access.log main; #location \[=|~|~*|^~\] /uri/ { … } # = 精确匹配 # ~ 正则匹配,区分大小写 # ~\* 正则匹配,不区分大小写 # ^~ 关闭正则匹配 #匹配原则: # 一、全部匹配分两个阶段,第一个叫普通匹配,第二个叫正则匹配。 # 二、普通匹配,首先经过“=”来匹配彻底精确的location # 2.一、 若是没有精确匹配到, 那么按照最大前缀匹配的原则,来匹配location # 2.二、 若是匹配到的location有^~,则以此location为匹配最终结果,若是没有那么会把匹配的结果暂存,继续进行正则匹配。 # 三、正则匹配,依次从上到下匹配前缀是~或~*的location, 一旦匹配成功一次,则马上以此location为准,再也不向下继续进行正则匹配。 # 四、若是正则匹配都不成功,则继续使用以前暂存的普通匹配成功的location. # 匹配任何查询,由于全部请求都已 / 开头。可是正则表达式规则和长的块规则将被优先和查询匹配。 location / { #定义服务器的默认网站根目录位置 root html; #默认访问首页索引文件的名称 index index.html index.htm; #反向代理路径 proxy_pass http://192.168.2.3/test; #反向代理的超时时间 proxy\_connect\_timeout 10; proxy_redirect default; } # auth\_request表示须要调用第三方接口进行受权,后台response.setStatus(200)返回时会继续走proxy\_pass 代理。 location /test { auth_request /auth; proxy_pass http://192.168.40.26:8080/cloud/wapi/v1.0/hello; } location /auth { proxy_pass http://192.168.40.26:8080/cloud/wapi/v1.0/auth/; #proxy\_pass\_request_body off; #proxy\_set\_header Content-Length ""; #proxy\_set\_header X-Original-URI $request_uri; } } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; proxy\_set\_header Host $host; proxy\_set\_header Referer $http_referer; proxy\_set\_header Cookie $http_cookie; proxy\_set\_header X-Real-IP $remote_addr; proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for; # 访问test2时,会经过lua脚本的access\_by\_lua调用后台方法v1.0/auth方法。 location /test2 { access\_by\_lua 'local res = ngx.location.capture("/v1.0/auth") if res.status == ngx.HTTP_OK then return end if res.status == ngx.HTTP_FORBIDDEN then ngx.exit(res.status) end ngx.exit(ngx.HTTP\_INTERNAL\_SERVER_ERROR) '; proxy_pass http://192.168.40.119:8888/ipam-server; } #调用后台方法v1.0/auth方法 时会被代理到192.168.40.26:8080/v1.0/auth上去。 location ~ /v1.0/(.*) { internal; proxy_pass http://192.168.40.26:8080; } #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 nginx.test2.com; # 经过 openssl 生成对应的证书 ssl_certificate /usr/local/src/nginx/ssl/nginx.crt; ssl\_certificate\_key /usr/local/src/nginx/ssl/nginx.key; ssl\_session\_cache shared:SSL:1m; ssl\_session\_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl\_prefer\_server_ciphers on; location = /favicon.ico { log\_not\_found off; access_log off; } # https的代理单点登陆 location /cas/login { proxy\_set\_header Host $host:$server_port; proxy\_set\_header Referer $http_referer; proxy\_set\_header Cookie $http_cookie; proxy\_set\_header X-Real-IP $remote_addr; proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for; proxy\_set\_header X-FORWARDED-HOST $server_addr; proxy\_set\_header X-FORWARDED-PORT $server_port; # 单点登陆的服务器的IP proxy_pass https://192.168.40.119:8090/cas/login; } # 登陆单点登陆时所需的css、js、jpg等文件 location ~*\\.(gif|jpg|jpeg|png|css|js|jsp|htm)$ { # 此处不能配置 https://192.168.40.119:8090/ 不然会报错 proxy_pass https://192.168.40.119:8090; } } }