nginx常用配置
重定向:
location / { proxy_pass http://cxb; if ( $request_uri = '/service/version/getLatestAppVersion/0' ) { rewrite ^/(.*) https://cxjapp.chexiang.com/service/version/getLatestAppVersion/0 redirect; } }
转换链接的几种方式:
Rewrite规则:使用nginx的rewrite模块,通过正则表达式匹配URL,将其重写为新的URL。
例如,将http://example.com/old-url重写为http://example.com/new-url。
Redirect规则:使用nginx的return或rewrite模块,将请求重定向到新的URL。
例如,将http://example.com/old-url重定向到http://example.com/new-url。
Proxy_pass规则:使用nginx的proxy_pass模块,将请求代理到另一个服务器或地址。
例如,将http://example.com/old-url代理到http://new-server.com/new-url。
Alias规则:使用nginx的alias模块,将请求映射到本地文件系统的另一个目录或文件。
例如,将http://example.com/old-url映射到本地文件系统的/var/www/new-url目录。
Map规则:使用nginx的map模块,将请求映射到另一个URL。
例如,将http://example.com/old-url映射到http://example.com/new-url。
websocket配置:
当前标准必须要借助HTTP协议的Upgrade协议头来升级为websocket协议再来进行websocket通信,也就是在建立连接时使用HTTP/1.1的101状态码进行协议切换将HTTP升级为ws协议。
upstream rcschexiang { server 10.32.215.46:8080 weight=1 max_fails=2; server 10.32.215.57:8080 weight=1 max_fails=2; } upstream rcschexiang-websocket { server 10.32.215.46:8036 weight=1 max_fails=2; server 10.32.215.57:8036 weight=1 max_fails=2; } server { listen 80; server_name www.scriptjc.com; # access_log logs/rcs.access.log main; # error_log logs/rcs.error.log notice; if ($request_method !~ ^(GET|POST|PUT|DELETE)$ ) { return 400; } if ( $uid_set != '' ){ set $saic_user_cookie $uid_set; } if ( $uid_got != '' ){ set $saic_user_cookie $uid_got; } location / { proxy_pass http://rcschexiang; # 要关闭selinux否者出现502 } location /socket.io { proxy_pass http://rcschexiang-websocket; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; # Upgrade: websocket proxy_set_header Connection "Upgrade"; proxy_set_header Host $host; } }
主要是这两行:
proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade";
或者:
server { listen 80; server_name www.scriptjc.com; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; proxy_http_version 1.1; proxy_connect_timeout 600; proxy_read_timeout 600; if ($request_method !~ ^(GET|POST|PUT|DELETE)$ ) { return 400; } if ( $uid_set != '' ){ set $saic_user_cookie $uid_set; } if ( $uid_got != '' ){ set $saic_user_cookie $uid_got; } location / { proxy_pass http://cxyun; } }
添加访问权限:
给予账号密码限制:
创建账号密码:
htpasswd -c -d /etc/nginx/conf.d/pass_file admin # 随后输入密码 htpasswd -d /etc/nginx/conf.d/pass_file admin1 # 继续添加账号
在nginx中配置:
location /rsmrest/synCaseOrder.json { auth_basic "entry user and password"; auth_basic_user_file /etc/nginx/conf.d/pass_file; # 主要是这个指令 proxy_pass http://upstreamname; }
给予ip限制:
location /rsmrest/synCaseOrder.json { allow 123.123.123.123; deny all; proxy_pass http://upstreamname; }
代理有密码的地址或upstream:解密。
server{ listen 80; server_name prometheus-k8s-prd.scriptjc.com; location / { proxy_set_header Authorization "Basic eDJHdjNxxxxxxxxxxxxxxx"; # 把用户名密码转换成Basic串 proxy_pass http://prometheus-k8s-prd.demo.com; } }
http跳转https:
server { listen 80; server_name www.scriptjc.com user.scriptjc.com; rewrite ^(.*)$ https://$host$1 permanent; }
上传文件大小限制:
http { sendfile on; # 开启文件上传 client_max_body_size 10M; # 文件上传大小限制 }
获取客户端真实IP:
方法一:
nginx将从IP地址范围为192.168.1.0/24的代理服务器中读取真实IP地址,并将其存储在$remote_addr变量中。
http { # ... set_real_ip_from 192.168.1.0/24; real_ip_header X-Forwarded-For; # ... }
跨域:server段和location段都可以。
add_header 'Access-Control-Allow-Origin' '*';
路径重写:
location /max { rewrite ^/max/(.*)$ /$1 break; proxy_pass http://192.168.1.123; }
方法二:
使用 nginx 将真实的远程 IP 转发到代理服务器:
server { listen 80; server_name example.com; location / { proxy_pass http://backend; proxy_set_header X-Real-IP $remote_addr; # 保留客户端远程IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #多级代理时会包含真实客户端及中间每个代理服务器的IP } # other server blocks and directives } upstream backend { server backend.example.com; }
日志轮换:
方法一:nginx自带logrotate模块,"--with-http_log_module"和"--with-http_log_module=dynamic",使用如下命令查看。
nginx -V
在nginx.conf中配置:
http { ... logrotate daily rotate 5 size 10M compress delaycompress notifempty create 0644 nginx nginx; ... }
方法二:使用linux通用的日志轮换工具logrotate,创建如下配置文件。
vim /etc/logrotate.d/nginx
内容:
/var/log/nginx/*.log { daily rotate 5 missingok compress delaycompress notifempty create 0640 nginx nginx sharedscripts postrotate /usr/sbin/nginx -s reopen endscript }
或者:两种配置效果相同,-USR1表示给nginx发了个信号重新打开文件。
/var/log/nginx/*.log { create 0640 nginx root daily rotate 60 missingok notifempty compress dateext # 以日期为结尾 dateformat -%Y-%m-%d # 日期的格式 delaycompress sharedscripts postrotate /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true endscript }
检查配置:
logrotate -d /etc/logrotate.d/nginx
手动执行轮换:-v显示详情,-f强制轮换
logrotate -v /etc/logrotate.d/nginx
加定时任务:
59 23 * * * logrotate /etc/logrotate.d/nginx