基础配置
设置worker进程的用户
# 指的linux中的用户,会涉及到nginx操作目录或文件的一些权限,默认为nobody
user nobody
worker进程工作数设置
# 一般来说CPU有几个,就设置几个,或者设置为N-1也行
worker_processes 5; ## Default: 1
error日志打印位置
# (日志打印级别[ debug | info | notice | warn | error | crit ])
error_log /var/log/nginx/error.log
设置nginx进程 pid
pid logs/nginx.pid;
设置工作模式
events {
# 默认使用epoll [ kqueue | rtsig | epoll | /dev/poll | select | poll ] ;
use epoll;
# 每个worker允许连接的客户端最大连接数, 默认1024
worker_connections 10240;
}
http
# http 是指令块,针对http网络传输的一些指令配置
http {
}
include 引入外部配置,提高可读性,避免单个配置文件过大
+ include [mime.types](https://www.nginx.com/resources/wiki/start/topics/examples/full/#mime-types);
+ include [proxy.conf](https://www.nginx.com/resources/wiki/start/topics/examples/full/#proxy-conf);
+ include [fastcgi.conf](https://www.nginx.com/resources/wiki/start/topics/examples/full/#fastcgi-conf);
设定日志格式
# main为定义的格式名称,如此access_log就可以直接使用这个变量了
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
| 参数名 | 参数意义 |
|---|---|
| $remote_addr | 客户端ip |
| $remote_user | 远程客户端用户名,一般为:’-’ |
| $time_local | 时间和时区 |
| $request | 请求的url以及method |
| $status | 响应状态码 |
| $body_bytes_send | 响应客户端内容字节数 |
| $http_referer | 记录用户从哪个链接跳转过来的 |
| $http_user_agent | 用户所使用的代理,一般来时都是浏览器 |
| $http_x_forwarded_for | 通过代理服务器来记录客户端的ip |
sendfile使用高效文件传输, 提升传输性能。
sendfile on;
# 启用后才能使用 `tcp_nopush`,是指当数据表累积一定大小后才发送,提高了效率
tcp_nopush on;
keepalive_timeout设置客户端与服务端请求的超时时间
# 保证客户端多次请求的时候不会重复建立新的连接,节约资源损耗
keepalive_timeout 56;
gzip压缩
# 开启压缩功能,目的:提高传输效率,节省带宽
gzip on
# 限制最小压缩,小于1字节的不会压缩
gzip_min_length 1;
# 压缩比(文件越大,压缩越多,但是使用cpu越多, 1-9)
gzip_comp_level 3
# 定义压缩文件的类型
gzip_types text/plain application/javascript;
location路由
路由匹配
# 精确匹配
location = / {
[ configuration A ]
}
# 正则匹配 (/index.html)
location / {
[ configuration B ]
}
# 正则匹配 (/documents/document.html)
location /documents/ {
[ configuration C ]
}
# ^: 以...开头, ~: 区分大小写 (/images/1.gif)
location ^~ /images/ {
[ configuration D ]
}
# ~*: 不区分大小写, $: 以...结尾 (/documents/1.jpg)
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
常用配置指令
- alias别名配置
用于访问文件系统,在匹配到location配置的URL路径后,指向(
替换为)alias配置的路径,如:
location /i/ {
alias /data/w3/images/;
}
请求/i/top.gif(省略了协议和域名),将会返回文件/data/w3/images/top.gif。
- root 根路径配置
用于访问文件系统,在匹配到location配置的URL路径后,指向root配置的路径,并把请求路径
附加到其后,如:
location /i/ {
root /data/w3;
}
请求/i/top.gif(省略了协议和域名),将会返回文件/data/w3/i/top.gif。
- proxy_pass 反向代理配置 用于代理请求,适用于前后端负载分离或多台机器、服务器负载分离的场景,在匹配到location配置的URL路径后,转发请求到proxy_pass配置额URL,是否会附加location配置路径与proxy_pass配置的路径后是否有"/"有关,有"/"则不附加,如:
location /test/ {
proxy_pass http://127.0.0.1:8080/;
}
请求/test/1.jpg,将会被nginx转发请求到http://127.0.0.1:8080/1.jpg(未附加/test/路径)
完整样例
user www www; ## Default: nobody
worker_processes 5; ## Default: 1
# [ debug | info | notice | warn | error | crit ]
error_log logs/error.log;
pid logs/nginx.pid;
worker_rlimit_nofile 8192;
events {
# use [ kqueue | rtsig | epoll | /dev/poll | select | poll ] ;
use epoll;
worker_connections 4096; ## Default: 1024
}
http {
include conf/mime.types;
include /etc/nginx/proxy.conf;
include /etc/nginx/fastcgi.conf;
index index.html index.htm index.php;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
server_names_hash_bucket_size 128; # this seems to be required for some vhosts
server { # php/fastcgi
listen 80;
server_name domain1.com www.domain1.com;
access_log logs/domain1.access.log main;
root html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:1025;
}
}
# 反向代理
server { # simple reverse-proxy
listen 80;
server_name domain2.com www.domain2.com;
access_log logs/domain2.access.log main;
# serve static files
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/big.server.com/htdocs;
expires 30d;
}
# pass requests for dynamic content to rails/turbogears/zope, et al
location / {
proxy_pass http://127.0.0.1:8080;
}
}
# 负载均衡
upstream big_server_com {
server 127.0.0.3:8000 weight=5;
server 127.0.0.3:8001 weight=5;
server 192.168.0.1:8000;
server 192.168.0.1:8001;
}
server { # simple load balancing
listen 80;
server_name big.server.com;
access_log logs/big.server.access.log main;
location / {
proxy_pass http://big_server_com;
}
}
}