1.全局块:
主要设置一些影响nginx 服务器整体运行的配置指令。如:用户(组),生成worker process 数,日志存放路径,配置文件引入,pid文件路径
2.events 块
影响nginx 服务器与用户的网络连接。如:每个worker processs 的最大连接数,事件驱动模型
3.http块
nginx 配置中的重要部分,代理,缓存等功能都可以在此配置。如:文件引入,mime-type 定义,连接超时,单连接请求数上限
4.server 块
与“虚拟主机”的概念有密切关系。每个server 块相当于一台虚拟主机,最常见的两个配置项是监听配置和虚拟主机的名称或IP配置
5.location 块
在server 块中,可以有多个。地址定向,数据缓存,和应答控制等功能都是在这部分实现。
- listen address[:port]; #配置网络监听
listen *:80 | *:8080 | 8080; #监听所有的80 和 8080 端口
listen 192.168.1.1:8080; #监听192.168.1.1地址的8080端口
listen 192.168.1.11; #监听192.168.1.11 的所有端口
- server_name name ....; #虚拟主机配置
server_name www.xx.com yy.com; #虚拟主机名为www.xx.com 和 yy.com
server_name *xx.com www.yy.*; #可以使用通配符,但只能放在首段或尾段
server_name ~^www\d+\.xx\.com$ #可以使用正则表达式,但必须使用 ~ 开头
匹配规则按:准确匹配,通配符(开始)匹配 ,通配符(结尾)匹配,正则匹配
- location [ = | ~ | ~* | ^~ ] uri {...} #位置配置
在一不使用 =、~、~*、^~ 时的匹配规则:先搜索【标准的uri】如果有多个可以匹配,那么就记录匹配度最高的一个。然后再搜索【正则uri】,当第一个【正则uri】匹配成功时就结束搜索并使用它。如果【正则uri】都没有匹配到,就使用【标准uri】就是刚搜索的那个。
【=】 : 只能用在【标准uri】前,如果匹配成功了,就停止搜索并使用它。
【~ | ~*】:只能用在【正则uri】前,前面区分大小写,后面不区分大小写
【^~】: 只能用在【标准uri】前,搜索所有【标准uri】找到匹配度最高的一个结束搜索并使用它
- root path; 配置请求的根目录
path 为服务器资源的根目录路径,如:
location /data/ {
root /usr/local/web;
}
当请求为 /data/index.htm 时, 将映射至 /usr/local/web/data/index.hm
- alias path; 更改location 的URI
location ~ ^/data/(.+\.(htm|htm))${
alias /usr/local/app/$1;
}
当请求为 /data/index.htm 时, 将映射至 /usr/local/app/index.htm
- index file ...; 设置网站的默认首页
- error_page code ... uri #设置网站的错误页面
error_page 404 /404.htm
error_page 403 http://www.xx.com/403.htm
error_page 410 =301 /fes.htm # 当服务器产生410时,返回 fes.htm ,并客户端http status 为 301
- allow address | CIDR | all; # 基于ip配置nginx 的访问权限 - 允许
- deny address | CIDR | all; # 基于ip配置nginx 的访问权限 - 拒绝
location / {
deny 192.168.1.10;
allow 192.168.2.45;
allow 192.168.3.78;
deny all;
}
遇到 deny 或 allow 指定是按顺序对当前客户端的连接进行访问权限检查 - auth_basic string | off; 基于密码配置nginx 的访问权限
- auth_basic_user_file file; 使用文件来配置用户名和密码
#=========================全局块============================
#user user [grop]; # 指定可以运行nginx 服务器的用户及组。只被设置的用户及组才有权限管理
#user nobody nobody; # 所用用户可用,默认配置
user nginx nginx;
#worker_processes number | auto; #控制并发处理,值越大,支持的并发数越多
#worker_processes 1; #默认值 ,最多产生1个worker_processes
worker_processes auto;
#pid file; #pid 存放路径.相对或绝对路径
#pid logs/nginx.pid; #默认在安装目录logs/nginx.pid
pid /var/run/nginx.pid;
#error_log file | stder [debug | info | notice | warn | error | crit | alert | emerg]; #错误日志存放路径
#error_log logs/error.log error; # 可在 全局块,http 块 ,serveer 块,location 块中配置
error_log /data/wwwlogs/error_nginx.log crit;
#include file; #配置文件引入,可以是相对/绝对路径。 指令可以放在配置文件的任意地方(任意块中)
worker_rlimit_nofile 51200;
#===========================events 块===============================
events {
#accept_mutex on|off; #网络连接序列化。解决“惊群”问题
accept_mutex on; #默认为开启状态
#multi_accept on|off; #是否允许worker process同时接收多个网络连接,
multi_accept off; #默认为关闭
#use method; #事件驱动模型选择,select 、 poll 、 kqueue 、 epoll 、 rtsig 、 /dev/poll 、 eventport
use epoll;
#worker_connections number; #worker process 的最大连接数。
worker_connections 512; #默认值为512。注意设置不能大于操作系统支持打开的最大文件句柄数量。
}
#==========================http块============================
http {
#定义 mime-type (网络资源的媒体类型)。 指定能识别前端请求的资源类型
include mime.types; #引入外部文件 ,在外部文件中有定义types 块
default_type application/octet-stream; #默认为 text/plain 。 http块、server块、location块中可配置
#access_log path [format[buffer=size]]; #定义服务日志,记录前端的请求日志
#access_log logs/access.log combined; #combined 是默认定义的日志格式字符串名称
access_log off; #关闭日志
#log_format name stirng #定义了日志格式,以便access_log 使用
#log_format combined '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#sendfile on|off; #配置允许 sendfile 方式传输文件
sendfile off; #默认关闭,可在http块、server块、location块 中定义
#sendfile_max_chunk size; #配置worker process 每次调用sendfile()传输的数据最最大值
#sendfile_max_chunk 0; #默认为0 不限制。 可在http块、server块、location块 中定义
sendfile_max_chunk 128k;
#keepalive_timeout timeout [header_timeout]; #配置连接超时时间, 可在http块、server块、location块 中定义
#keepalive_timeout 75s; #默认为75秒,与用户建立会话连接后,nginx 服务器可以保持这些连接打开的时间。
keepalive_timeout 120 100s; #在服务器端保持连接的时间设置为120s,发给用户端的应答报文头部中keep-alive 域的设置为100s
#keepalive_requests number; #单连接请求数上限
keepalive_requests 100; #默认为 100
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 1024m;
tcp_nopush on;
server_tokens off;
tcp_nodelay on;
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;
#Gzip Compression
gzip on;
gzip_buffers 16 8k;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types
text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
text/javascript application/javascript application/x-javascript
text/x-json application/json application/x-web-app-manifest+json
text/css text/plain text/x-component
font/opentype application/x-font-ttf application/vnd.ms-fontobject
image/x-icon;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
#If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
######################## default ############################
# server {
# listen 8067;
# server_name _;
# access_log /data/wwwlogs/access_nginx.log combined;
# root /data/wwwroot/default;
# index index.html index.htm index.jsp;
# location /nginx_status {
# stub_status on;
# access_log off;
# allow 127.0.0.1;
# deny all;
# }
# location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
# expires 30d;
# access_log off;
# }
# location ~ .*\.(js|css)?$ {
# expires 7d;
# access_log off;
# }
# location ~ {
# proxy_pass http://127.0.0.1:8080;
# include proxy.conf;
# }
# }
# server{
# listen 8087;
# server_name _;
# root /home/wwwroot/default;
# location / {
# }
# }
upstream backend{
server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=2;
server 139.224.209.104:8080 backup;
}
server{
listen 80;
listen 443 ssl;
server_name wmcenter.xy-asia.com;
#ssl on;
default_type 'text/html';
charset utf-8;
ssl_certificate /usr/local/cert/1634560_wmcenter.xy-asia.com.pem;
ssl_certificate_key /usr/local/cert/1634560_wmcenter.xy-asia.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
root html;
index index.html index.htm;
location ~ .*\.(js|css)?$
{
expires 1h;
proxy_pass http://backend;
}
location / {
proxy_pass http://backend;
add_header backendIP $upstream_addr;
#proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
#proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header X-Forwarded-Proto https;
#proxy_redirect http://http://127.0.0.1:8080 $scheme://127.0.0.1:8080;
#port_in_redirect on;
#proxy_redirect off;
include proxy.conf;
}
#error_log /home/wwwlogs/xywm.log;
}
#缓存配置
proxy_temp_file_write_size 264k;
proxy_temp_path /data/wwwlogs/nginx_temp;
proxy_cache_path /data/wwwlogs/nginx_cache levels=1:2 keys_zone=prc:200m inactive=5d max_size=400m;
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
########################## vhost #############################
include vhost/*.conf;
}