官网地址
nginx官网
http://nginx.org/
转发数据库、mq等端口
stream {
upstream back{
server 192.168.208.1:3000;
server 192.168.208.2:3000;
}
server {
listen 2000;
proxy_connect_timeout 2s;
proxy_timeout 300s;
proxy_pass back;
}
server {
listen 5000;
proxy_connect_timeout 2s;
proxy_timeout 300s;
proxy_pass 192.168.208.2:3000;
}
}
可转发IP、端口、路径的配置
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Path $request_uri;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
$http_host:用于获取客户端请求的Host头的值,常用于反向代理配置中将客户端的Host头传递给后端服务器。
$host:用于Nginx内部处理,表示解析后的主机名,通常与$http_host的值相同。
$proxy_host:用于反向代理配置中,表示后端服务器的地址,而不是客户端请求的Host头。
在实际应用中,需要根据具体的需求和场景选择合适的变量。
如果需要将客户端的Host头传递给后端服务器,应该使用$http_host;
如果需要指定后端服务器的地址,应该使用$proxy_host。
https://nginx.org/en/docs/http/ngx_http_proxy_module.html
nginx配置参数
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
nginx配置
location ^~ /url_prefix/ {
proxy_redirect off;
add_header Cache-Control no-cache;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080;
proxy_connect_timeout 180;
proxy_send_timeout 180;
proxy_read_timeout 180;
}
location ^~ /web {
add_header Access-Control-Allow-Origin *;
alias /opt/web;
index index.html index.htm;
}
location ^~ /web {
root /usr/share/nginx/html;
index index.html index.htm;
<!--未找到文件时跳转地址-->
try_files $uri $uri/ /lweb/index.html;
}
nginx配置重定向和location同级
rewrite ^/api/gateway/model-haifa
http://www.baidu.com permanent;
nginx编译安装
1、检查和安装nginx安装依赖项
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
2、进入nginx目录执行
./configure --prefix /opt/nginx
3、编译、安装
make && make install
设置nginx开机自动运行
vi /etc/rc.d/rc.local
添加启动命令
/opt/nginx/sbin/nginx
nginx查询步骤
1、查看启动master进程pid
ps -ef|grep nginx
2、查询进程
ll /proc/pid/
2、查询进程启动路径
ll /proc/pid/exe
3、查询进程的启动地址
readlink -f /proc/pid/cwd
nginx命令
nginx -v
nginx -V
nginx
nginx -c fileName
nginx -s reopen
nginx -s reload
nginx -s quit
nginx -s stop
killall nginx
nginx -t
nginx -t -c /etc/nginx/nginx.conf
systemctl stop/start/restart nginx
rpm -ql nginx,qpm -qa nginx
nginx安装支持stream的模块
yum动态添加 --with-stream --with-stream_ssl_module
yum install -y nginx-mod-stream 仅安装stream模块
yum install -y nginx-all-modules 安装所有动态模块
--with-stream=dynamic stream模块是动态加载的模块
--modules-path=/usr/lib64/nginx/module 查看已安装的模块位置
编译安装的nginx支持stream模块
https:
https:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)