1. 配置文件构成
Nginx的配置文件nginx.conf位于安装目录的conf目录下.
nginx.conf由多个块组成,最外面的块是main(即文件本身),main下面又包含三个模块,层级结构如下:
全局块 # 设置工作进程数,定义日志路径等
events块 # 设置处理轮询事件模型,每个工作进程最大连接数及http层的keep-alive超时时间
http块 # 路由匹配、静态文件服务器、反向代理、负载均衡等
多个upstream # 负载均衡服务器设置
多个server # 指定主机和端口
多个location # 匹配网页位置
这四者之间的关系式:
server继承main,location继承server,upstream既不会继承其他设置也不会被继承
2. Nginx配置文件详解
# ======↓↓↓ 全局块 ↓↓↓======
# 默认账号
#user nobody;
# 指定Nginx要开启的进程数,默认1,建议指定和CPU的数量一致即可
worker_processes 1;
# 定义全局错误日志文件:
# 日志输出级别有debug、info、notice、warn、error、crit可供选择
# 其中,debug输出日志最为最详细,而crit输出日志最少
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# 指定进程pid的存储文件位置
#pid logs/nginx.pid;
# ======↑↑↑ 全局块 ↑↑↑======
# ======↓↓↓ events块 ↓↓↓======
# 设定Nginx的工作模式及连接数上限
events {
# 定义Nginx每个进程的最大连接数,默认是1024
worker_connections 1024;
}
# ======↑↑↑ events块 ↑↑↑======
# ======↓↓↓ http块 ↓↓↓======
http {
# 实现对配置文件所包含的文件的设定,可以减少主配置文件的复杂度
include mime.types;
# 默认文件类型
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
# 访问日志文件路径,访问日志存放路径,main用于指定访问日志的输出格式
#access_log logs/access.log main;
# 启动高效传输文件的模式
sendfile on;
#tcp_nopush on;
# 客户端连接保持活动的超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
# 开启页面压缩模式
#gzip on;
# 虚拟主机配置01
server {
# 监听的端口号,可同时监听多个端口号
listen 8080;
listen 8180;
# 监听的IP地址或域名,多个域名之间用空格分开,支持通配符 *.baidu.com、www.baidu.*
server_name 127.0.0.1 baidu.com;
#charset koi8-r;
# 虚拟主机的访问日志存放路径,main用于指定访问日志的输出格式
#access_log logs/host.access.log main;
# URL地址匹配,支持正则表达式匹配,也支持条件判断匹配
# 用户可以通过location指令实现Nginx对动、静态网页进行过滤处理
# 使用location URL匹配配置还可以实现反向代理,用于实现动态解析或负载均衡
location / {
# 指定虚拟主机的网页根目录,可以是相对路径,也可以是绝对路径
root html;
# 设定访问的默认首页地址
index index.html index.htm;
}
#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
# 混合使用基于IP、名称和端口的配置
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
# 虚拟主机配置02:HTTPS虚拟主机
server {
# 监听的端口号
listen 443 ssl;
# 监听的IP地址或域名,多个域名之间用空格分开,支持通配符 *.baidu.com、www.baidu.*
server_name baidu.com;
# ssl证书的pem文件路径
ssl_certificate /home/cert.pem;
# ssl证书的key文件路径
ssl_certificate_key /home/cert.key;
# 设置共享会话缓存大小,默认1m
ssl_session_cache shared:SSL:10m;
# 设置会话超时时间
ssl_session_timeout 5m;
# 配置SSL加密算法,默认"HIGH:!aNULL:!MD5"
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
# 优先采取服务器算法
ssl_prefer_server_ciphers on;
# 匹配网页位置
location / {
# 请求转发到指定服务
proxy_pass http://127.0.0.1:8000/;
# ======↓↓↓ 设置请求头 ↓↓↓======
# $host、$http_host是原始的"HOST"字段;$proxy_host是proxy_pass后的;
# 如果客户端发过来的请求的header中没有有’HOST’这个字段时,建议使用$host
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# ======↑↑↑ 设置请求头 ↑↑↑======
}
}
# 虚拟主机配置03:HTTP虚拟主机
server {
# 监听的端口号,可同时监听多个端口号
listen 80;
listen 81;
# 监听的IP地址或域名,多个域名之间用空格分开,支持通配符 *.baidu.com、www.baidu.*
server_name baidu.com;
# http请求转成https
return 301 https://$server_name$request_uri;
}
# 负载均衡配置,定义一个本机域名"website01.com"(需符合url地址规则,不能有"_"等特殊符号)
upstream website01.com {
# 当前server负载权重,weight表示权重越大接收请求越多,默认为1;
# fail_timeout秒内失败max_fails次则将此节点从及群众剔除,fail_timeout秒后重亲启用
server 192.168.66.6:8080 weight=3 max_fails=3 fail_timeout=15;
server 192.168.66.7:8080 weight=1 max_fails=3 fail_timeout=15;
}
# 虚拟主机配置04:负载均衡虚拟主机
server {
# 监听的端口号,可同时监听多个端口号
listen 83;
listen 84;
# 监听的IP地址或域名,多个域名之间用空格分开,支持通配符 *.baidu.com、www.baidu.*
server_name baidu.com;
# 匹配网页位置
location / {
# 转发到负载均衡集群,"website01.com"是负载均衡配置的本地域名
proxy_pass http://website01.com;
}
}
# ======↓↓↓ 实用示例 ↓↓↓======
# 负载均衡配置"website02.com",名称自定义即可(需符合url地址规则,不能有"_"等特殊符号)
upstream website02.com {
# 当前server负载权重,weight表示权重越大接收请求越多,默认为1;
server 192.168.1.196 weight=1;
server 192.168.1.196:8280 weight=1;
}
# 虚拟主机配置05
server {
# 监听80端口和81端口
listen 86;
listen 87;
# 监听IP地址或域名,多个域名之间用空格分开,支持通配符 *.baidu.com、wwdw.baidu.*
server_name 192.168.1.198;
location /docs {
# 转发到负载均衡集群,"website02.com"是负载均衡配置的本地域名
proxy_pass http://website02.com;
}
location /docs/config {
# 转发的地址后面没有以"/"结尾,代理的目标url后面直接组合匹配的地址,相当于直接替换了URL+端口号
# 如 http://192.168.1.198/docs/config/ 实际代理的目标url是 http://192.168.1.196:8280/docs/config/
proxy_pass http://192.168.1.196:8280;
}
# 匹配的地址建议以"/"结尾
location /api/ {
# 转发的地址后面以"/"结尾,代理的目标url会忽略掉匹配的地址,相当于替换了URL+端口号并忽略掉匹配的内容
# 如 http://192.168.1.198/api/docs/config/ 实际代理的目标url是 http://192.168.1.196/docs/config/
proxy_pass http://192.168.1.196/;
}
# 转发到http://678910.top
location / {
proxy_pass http://678910.top;
}
}
# ======↑↑↑ 实用示例 ↑↑↑======
}
# ======↑↑↑ http块 ↑↑↑======
负载均衡
实例配置
3. 扩展
3.1 root、index和proxy_pass优先级
如果server的location中同时配置了root、index和proxy_pass,那么proxy_pass生效,即proxy_pass的优先级高于root和index,如以下配置,会跳转到baidu.com,而不会去访问html/index.html
# 此配置会跳转到http://baidu.com,因为proxy_pass权限等级最高,优先生效
location / {
# 指定虚拟主机的网页根目录,可以是相对路径,也可以是绝对路径
root html;
# 设定访问的默认首页地址
index index.html index.htm;
# 请求转发到指定服务
proxy_pass http://baidu.com;
}
3.2 静态文件配置
Nginx配置静态文件的命令有两个:root和alias,区别如下: 1. root是指定项目的根目录,适用与server和location,可以指定多个,如果locaiton没有指定,会往其外层的server或http中寻找继承 2. root 的URL处理结果是: root路径 + location路径 3. alias是一个目录别名的定义,alias则只能在location中寻找 4. alias的URL处理结果是: 使用alias路径替换location路径 5. alias的URL必须以"/"结尾,否则会找不到文件;而root则可有可无,但建议以"/"结尾
# [root]
# 语法 默认值 配置段
root path root html http、server、location、if
# [alias]
# 语法 默认值 配置段
alias path 无 location
文件层级关系
opt
└─webSite
└─testAdmin
├─dist
├─img
│ photo1.png
│ photo2.png
└─js
test.js
demo.js
3.2.1 root配置示例
server {
# ip和端口一起设定
listen 192.168.1.198:83;
# 直接指定文件更目录,也可放置在location里面,建议以"/"结尾
root /opt/webSite/testAdmin/;
# 直接指定默认首页
index index.html;
location /img/ {
# 建议转发地址以"/"结尾
root /opt/webSite/testAdmin/dist/;
}
location /js/ {
# 建议转发地址以"/"结尾
root /opt/webSite/testAdmin/dist/;
}
}
地址匹配: root的path无论是否以"/"结尾,都会追加location的匹配内容到root的path后面,但如果匹配的内容不是以"/"开头,则可能出现追加后的地址不正确问题,因此建议这里以"/"结尾。 如果请求地址无法匹配location,则会自动去找server里面root的path进行追加
测试请求:
# 能匹配上location的字段,与location中root的path进行URL组合
http://192.168.1.198:83/img/photo2.png
http://192.168.1.198:83/js/demo.js
# 没有匹配上location的字段,因此与server中root的path进行URL组合
http://192.168.1.198:83/dist/img/photo2.png
http://192.168.1.198:83/dist/js/demo.js
3.2.3 alias配置示例
server {
# ip和端口一起设定
listen 192.168.1.198:86;
# 直接指定文件更目录,也可放置在location里面,建议以"/"结尾
root /opt/webSite/testAdmin/;
# 直接指定默认首页
index index.html;
location /img/ {
# 必须以"/"结尾
alias /opt/webSite/testAdmin/dist/img/;
}
location /js/ {
# 必须以"/"结尾
alias /opt/webSite/testAdmin/dist/js/;
}
}
地址匹配: alias则只能在location中寻找.alias的URL处理结果是:使用alias路径替换location路径;alias的URL必须以"/"结尾,否则会找不到文件.
测试请求:
# 能匹配上location的字段,与location中root的path进行URL组合
http://192.168.1.198:86/img/photo1.png
http://192.168.1.198:86/js/demo.js
# 没有匹配上location的字段,因此与server中root的path进行URL组合
http://192.168.1.198:86/dist/img/photo1.png
http://192.168.1.198:86/dist/js/demo.js