一、Nginx配置文件的"三大块"
全局块:
主要设置一些影响nginx服务器整体运行的配置指令,如:nginx服务器的用户(组)、worker process 数、pid等,其作用域是nginx服务器全局。
events块:主要设置一些影响nginx服务器与用户的网络连接的指令。
http块:可以涉及到代理、缓存、日志定义以及第三方模块的配置等。
server块:可以包含自己的全局块以及location块。在其全局块中最长见的配置是本虚拟主机(将服务器的某项或全部服务内容逻辑划分为多个服务单位,对外表现为多个服务器)的监听配置以及本虚拟主机的名称或IP配置。
location块:的设置主要涉及到地址定向、数据缓存以及应答控制等功能配置。
二、配置文件内容
#vim /etc/nginx/nginx.conf
#全局配置: user www www; # 定义Nginx运行的用户 和 用户组 worker_processes 8; #Nginx进程数, 建议设置为等于CPU总核心数 error_log /var/log/nginx/error.log info; #开启全局错误日志类型 pid /var/run/nginx.pid; #进程文件 worker_rlimit_nofile 65535; #文件打开数量#events配置: events{ use epoll; #事件驱动模型,使用epoll模型提高性能,多路复用 accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off worker_connections 65535; #单个进程最大连接数,默认为512 }#HTTP配置 http{ include mime.types; #扩展名与文件类型映射表 default_type application/octet-stream; #默认类型 sendfile on; #高效传输模式,默认为off,可以在http块,server块,location块。 sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。 tcp_nopush on; #防止网络阻塞 tcp_nodelay on; #防止网络阻塞 keepalive_timeout 65; #长连接超时时间,单位是秒 types_hash_max_size 2048; #设定请求缓存 client_header_buffer_size 32k; #上传文件大小限制 large_client_header_buffers 4 64k; #设定请求缓存 client_max_body_size 8m; #设定请求缓存 #隐藏响应头和错误通知版本号 server_tokens off; #第三方模块lua防火墙 lua_need_request_body on; lua_shared_dict limit 50m; lua_package_path "/application/nginx/conf/waf/?.lua"; init_by_lua_file "/application/nginx/conf/waf/init.lua"; access_by_lua_file "/application/nginx/conf/waf/access.lua"; #配置代理参数(可用于http,server,local)------------------------------------------------------------------------------------------------------------------- proxy_redirect default|off|redirect replacement; proxy_set_header Host $host; #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; #允许客户端请求的最大单文件字节数 client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数, proxy_connect_timeout 65; #nginx跟后端服务器连接超时时间(代理连接超时) proxy_send_timeout 65; #后端服务器数据回传时间(代理发送超时) proxy_read_timeout 65; #连接成功后,后端服务器响应时间(代理接收超时) proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 #缓存配置 proxy_cache_key '$host:$server_port$request_uri'; proxy_temp_file_write_size 64k; proxy_temp_path /dev/shm/JieLiERP/proxy_temp_path; proxy_cache_path /dev/shm/JieLiERP/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=5d max_size=1g; proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie; #网络------------------------------------------------------------------------------------------------------------------- #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。 fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; fastcgi_intercept_errors on; #指定是否传递4xx和5xx错误信息到客户端,或者允许nginx使用error_page处理错误信息。 #日志格式-------------------------------------------------------------------------------------------------------------------- log_format main '$remote_addr - $remote_user [$time_local] requesthost:"$http_host"; "$request" requesttime:"$request_time"; ' '$status $body_bytes_sent "$http_referer" - $request_body' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; error_log /var/log/nginx/error.log; #压缩(加快页面显示)-------------------------------------------------------------------------------------------------------------------- gzip on; #开启gzip压缩输出 gzip_min_length 1k; #最小压缩文件大小 gzip_buffers 16 64K; #压缩缓冲区 gzip_http_version 1.1; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0) gzip_comp_level 6; #压缩等级1-9 gzip_types text/plain application/x-javascript text/css application/xml application/javascript; #压缩的文件类型 gzip_vary on; #加个vary头,给代理服务器用的,有的浏览器不支持压缩 #负载池-------------------------------------------------------------------------------------------------------------------- #负载均衡配置 #轮询 upstream poo1 { server 192.168.1.1:80 max_fails=2 fail_timeout=30s; server 192.168.1.1:81 max_fails=2 fail_timeout=30s; } #权重 upstream poo2 { server 192.168.1.2:80 max_fails=2 fail_timeout=30s weight=1; server 192.168.1.2:81 max_fails=2 fail_timeout=30s weight=2; } #ip哈希 upstream poo3 { ip_hash; server 192.168.1.3:8080 max_fails=2 fail_timeout=30s; server 192.168.1.3:8080 max_fails=2 fail_timeout=30s; } #fair响应时间 upstream poo4 { server 127.0.0.1:8080 max_fails=2 fail_timeout=30s; server 192.168.8.203:8080 max_fails=2 fail_timeout=30s; fair; } #最小连接数 upstream poo5 { least_conn; server 127.0.0.1:80 max_fails=2 fail_timeout=30s; server 127.0.0.1:81 max_fails=2 fail_timeout=30s; }虚拟server-------------------------------------------------------------------------------------------------------------------- #虚拟服务器 server { listen 80; server_name www.baidu.com(要访问网站名/ip); rewrite ^/(.*) https://www.sina.com.cn permanent; #将所有对百度的请求都永久重定向到新浪上#反向代理-------------------------------------------------------------------------------------------------------------------- #访问nginx页面 location / { root html; #定义服务器的默认网站根目录位置,若在其他服务器上,则不写 index index.html index.php index.htm; #定义首页索引文件的名称,若在其他服务器上,则不写 } #访问Jenkins页面 location /jenkins/ { index index.html index.htm; proxy_pass http://poo2; #反向代理,请求跳转负载池poo2 } #静态文件,nginx自己处理 location ~ .*\.(js|css|ico|png|jpg|eot|svg|ttf|woff|static) { proxy_cache cache_one; proxy_cache_valid 200 304 302 5d; proxy_cache_valid any 5d; proxy_cache_key '$host:$server_port$request_uri'; add_header X-Cache '$upstream_cache_status from $host'; proxy_pass http://localhost; expires 30d; #缓存30天 } #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置. location ~ \.php$ { root /root; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name; include fastcgi_params; } #设定查看Nginx状态的地址 location /NginxStatus { stub_status on; access_log on; auth_basic "NginxStatus"; auth_basic_user_file conf/htpasswd; } #禁止访问 .htxxx 文件 location ~ /\.ht { deny all; }#其他页面反向代理到tomcat容器
location ~ .*$ { index index; proxy_pass http://zh-jieli.com; } }HTTPS认证:-------------------------------------------------------------------------------------------------------------------- #多个监听端口 server { listen 443; server_name static; charset utf-8; #gbk,utf-8,gb2312,gb18030 #可以实现多种编码识别 ssl on; ssl_certificate /etc/ssl/server.crt; #公钥文件(Globalsign颁发的证书) ssl_certificate_key /etc/ssl/server.key; #私钥文件 ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; #允许SSL协议 ssl_verify_client on; #开户客户端证书验证 ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; #加密算法 ssl_prefer_server_ciphers on; #启动加密算法 location / { root /usr/local/nginx/html; #定义服务器的默认网站根目录位置 index index.html index.php index.htm; #定义首页索引文件的名称 proxy_pass http://poo1; #反向代理,请求跳转负载池pool } #所有静态文件直接读取硬盘 location ~ .*\.(js|css|ico|png|jpg|eot|svg|ttf|woff) { root /var/lib/tomcat7/webapps/JieLiERP/WEB-INF ; expires 30d; #缓存30天 } #错误页面 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }