centos install
sudo yum update
yum install wget
wget http://nginx.org/download/nginx-1.14.2.tar.gz
tar -zxvf nginx-1.14.2.tar.gz
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
/usr/local/nginx/sbin/nginx -V
./configure --prefix=/usr/local/nginx --with-http_ssl_module
make && sudo make install
./sbin/nginx
SSL 配置
基础配置
worker_processes 1;
http {
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
# HTTPS server
server {
listen 443 ssl;
server_name wx.3bule.com;
ssl_certificate /etc/nginx/certs/wx.3bule.com.pem;
ssl_certificate_key /etc/nginx/certs/wx.3bule.com.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
}
nginx root alias
root: location + root
alias: alias 路径替换 location 必须要以/结尾
server {
listen 8090;
server_name 172.16.7.87;
location /jira {
root /data/soft/web/sailfish-jira/dist;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location / {
proxy_pass http://127.0.0.1:8090/jira/; # 一定要注意在末尾添加/
}
}
正则表达式
元字符(只能匹配一次, 利用重复) 重复
. 匹配除换行符以外的任意字符 * 重复0次或更多次
\w 匹配字母或数字或下划线或汉字 + 重复1次或更多次
\s 匹配任意空白字符 ? 重复0次或1次
\d 匹配数字 {n} 重复n次
\b 匹配单词的开始或结束 {n,} 重复n次或更多次
^ 匹配字符串的开始 {n, m} 重复n到m次
$ 匹配字符串的结束
() 分组与取值
# 原始url: /admin/website/article/35/change/uploads/party/5.jpg
# 转换后: /static/uploads/party/5.jpg
# 正则匹配url:
regular = /^\/admin\/website\/article\/(\d+)\/change\/uploads\/(\w+)\\(+w+)\.(png|jpg|gif|jpeg)$//
rewrite regular /static/uploads/$2/$3.$4 last;
server_name
- 可以跟多个域名,第1个是主域名
- 正则表达式, 加~前缀: ```nginx server { server_name ~^(www.)?(.+)$; location / { root/site/$2; } }
正则表达式创建变量 用小括号()
server {
server_name ~^(www.)?(?
```
- *泛域名: 仅支持在最前或最后
server_name *baidu.com
匹配顺序
- 精确匹配
- *在前的泛域名
- *在后的泛域名
- 按文件中的顺序匹配正则表达式域名
- default server (第1个 listen 指定default )
HTTP请求处理时的11个阶段
如何拿到真实的用户IP地址
- TCP连接四元组 (src ip, src port, dst ip, dst port)
- HTTP头部 X-Forwarded-For用于传递IP
- HTTP头部X-Real-IP 用于传递用户IP
- 网络中存在许多反向代理
Rewrite模块: return指令
Nginx自定义: 444 关闭连接
HTTP1.0标准: 301: http1.0永久重定向 302临时重定向,禁止被缓存
HTTP1.1标准:
303: 临时重定向,允许改变方法,禁止被缓存
307:临时重定向, 不允许改变方法,禁止被缓存
308: 永久重定向 不允许改变方法9ui