nginx配置
sh
vim /etc/nginx/nginx.conf
/usr/sbin/nginx -s reload
/usr/sbin/nginx -V
引入其他配置文件
include /etc/nginx/conf.d/*.conf;
域名到服务的转发
sh
server {
listen 80;
server_name a.hsianglee.cn;
location / {
proxy_pass http://127.0.0.1:3012;
}
}
server {
listen 80;
server_name localhost;
location /api/ {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080/;
}
}
获取用户真实ip
sh
server {
listen 80;
server_name a.hsianglee.cn;
location / {
proxy_pass http://127.0.0.1:3012;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
域名到静态文件的转发
sh
server {
listen 80;
server_name a.hsianglee.cn;
location / {
alias /web/aaa/;
index index.html;
try_files $uri $uri/ /index.html;
}
}
文件上传大小限制
sh
server {
listen 80;
server_name a.hsianglee.cn;
client_max_body_size 20m;
location / {
proxy_pass http://127.0.0.1:3012;
}
}
mysql端口转发
sh
stream {
upstream mysql_3306 {
server 127.0.0.1:3306;
}
server {
listen 3307;
proxy_connect_timeout 10s;
proxy_pass mysql_3306;
}
}
https配置
sh
server {
listen 443 ssl;
server_name a.hsianglee.cn;
ssl_certificate /etc/nginx/cert/a.hsianglee.cn_bundle.pem;
ssl_certificate_key /etc/nginx/cert/a.hsianglee.cn.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
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:3000;
client_max_body_size 4096m;
}
}
http重定向https
sh
server {
listen 80;
server_name localhost;
location / {
return 301 https://$host$request_uri;
}
}
子项目配置
try_files
配置/docs/index.html
则访问的是location /docs/
下的文件,如果配置/index.html
则直接访问的是location /
下的文件
sh
server {
listen 80;
server_name localhost;
location /docs/ {
alias /root/bin/epaas/platform-ui/epaas-platform-ui-docs/;
index index.html index.htm;
try_files $uri $uri/ /docs/index.html;
}
}
密码保护
vi /etc/nginx/conf.d/passwd
- 密码生成,文件的每一行的格式为: user:passwd,其中passwd为crypt(3)加密
sh
server {
listen 80;
server_name a.hsianglee.cn;
location / {
auth_basic "Restricted Area";
auth_basic_user_file /etc/nginx/conf.d/passwd;
alias /web/docs/;
index index.html;
try_files $uri $uri/ /index.html;
}
}
文件下载服务器
sh
server {
listen 80;
server_name a.hsianglee.cn;
charset utf-8;
location / {
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
alias /web/download/;
index index.html;
try_files $uri $uri/ /index.html;
}
}
负载均衡
sh
upstream testServer {
server localhost:8087 weight=10; # weight为权重,数值越大,概率越大
server localhost:8088 weight=2;
server localhost:8089;
}
server {
listen 80;
server_name a.hsianglee.cn;
location / {
proxy_pass http://testServer; # testServer 为自定义的服务器集群
proxy_set_header Host $host:$server_port;
}
}
防盗链
sh
location ~* {
valid_referers none blocked hsianglee.cn *.hsianglee.cn
if ($invalid_referer) {
return 403;
break;
}
access_log off;
}
路径省略.html
sh
server {
listen 80;
server_name a.hsianglee.cn;
root /web/aaa/;
location / {
if ($request_uri ~ ^/(.*)\.html$) {
return 302 /$1$args;
}
index index.html;
try_files $uri $uri.html $uri/ =404;
}
}
压缩
sh
gzip on; #决定是否开启gzip模块,on表示开启,off表示关闭;
gzip_min_length 10k; #设置允许压缩的页面最小字节(从header头的Content-Length中获取) ,当返回内容大于此值时才会使用gzip进行压缩,以K为单位,当值为0时,所有页面都进行压缩。建议大于1k
gzip_buffers 4 16k; #设置gzip申请内存的大小,其作用是按块大小的倍数申请内存空间,param2:int(k) 后面单位是k。这里设置以16k为单位,按照原始数据大小以16k为单位的4倍申请内存
gzip_http_version 1.1; #识别http协议的版本,早起浏览器可能不支持gzip自解压,用户会看到乱码
gzip_comp_level 6; #设置gzip压缩等级,等级越底压缩速度越快文件压缩比越小,反之速度越慢文件压缩比越大;等级1-9,最小的压缩最快 但是消耗cpu
gzip_types text/html text/css application/javascript application/xml; #设置需要压缩的MIME类型,非设置值不进行压缩,即匹配压缩类型
gzip_vary on; #启用应答头"Vary: Accept-Encoding"
root和alias区别
- 请求
/aaa/bbb/c.txt
,会返回服务器的/data/www/aaa/bbb/c.txt
sh
location ^~ /aaa/bbb/ {
root /data/www/;
}
- 请求
/aaa/bbb/c.txt
,会返回服务器的/data/www/c.txt
sh
location ^~ /aaa/bbb/ {
alias /data/www/;
}
- 总结
- root的处理结果是:root路径+location路径
- alias的处理结果是:使用alias路径替换location路径
- alias是一个目录别名的定义,root则是最上层目录的定义
- 使用alias时,目录名后面一定要加"/"
- alias只能位于location块中
配置
- 重定向
sh
location /system {
rewrite ^/(.*) http://$server_addr:3001/system/login permanent;
}
变量
- $args: 该变量中存放了请求URL中的请求指令。比如 http://127.0.0.1:3001?arg1=value1&arg2=value2 中的"arg1=value1&arg2=value2"。
- $content_length: 该变量中存放了请求头中的Content-length字段。
- $content_type: 该变量中存放了请求头中的 Content-type字段。
- $document_root: 该变量中存放了针对当前请求的根路径。
- $document_uri: 该变量中存放了请求的当前URI, 但是不包括请求指令。比如 http://xxx.abc.com/home/1?arg1=value1&arg2=value2; 中的 "/home/1"
- $host: 变量中存放了请求的URL中的主机部分字段,比如http://xxx.abc.com:8080/home中的 xxx.abc.com.
- $http_host: 该变量与$host唯一区别带有端口号:比如上面的是 xxx.abc.com:8080
- $http_user_agent: 变量中存放客户端的代理信息。
- $http_cookie, 该变量中存放客户端的cookie信息。
- $remote_addr 该变量中存放客户端的地址。
- $remote_port 该变量中存放了客户端与服务器建立连接的端口号。
- $remote_user 变量中存放客户端的用户名。
- $request_body_file 变量中存放了发给后端服务器的本地文件资源的名称
- $request_method 变量中存放了客户端的请求方式,比如 'GET'、'POST'等。
- $request_filename 变量中存放了当前请求的资源文件的路径名。
- $request_uri 变量中存放了当前请求的URI,并且带请求指令。
- $query_string 和变量$args含义一样。
- $scheme 变量中存放了客户端请求使用的协议,比如 'http', 'https'等。
- $server_protocol 变量中存放了客户端请求协议的版本, 比如 'HTTP/1.0'、'HTTP/1.1' 等。 https://www.cnblogs.com/tugenhua0707/p/10798762.html
获取url变量
sh
server {
listen 80;
types{}
default_type text/html;
return 200 $arg_name;
}
curl '10.0.1.200?name=zze&job=student' zze