Nginx

官网地址

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;
header设置传递访问域名到后端
#下面这两项需要一起设置通过X-Forwarded-Host才可以获取到正确的域名信息
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $http_host;
#后端获取http或https
proxy_set_header X-Forwarded-Proto $scheme;

$http_host:用于获取客户端请求的Host头的值,常用于反向代理配置中将客户端的Host头传递给后端服务器。

$host:用于Nginx内部处理,表示解析后的主机名,通常与$http_host的值相同。

$proxy_host:用于反向代理配置中,表示后端服务器的地址,而不是客户端请求的Host头。

在实际应用中,需要根据具体的需求和场景选择合适的变量。
如果需要将客户端的Host头传递给后端服务器,应该使用$http_host;
如果需要指定后端服务器的地址,应该使用$proxy_host#nginx header参数说明
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;                           #保留代理之前的host
 proxy_set_header X-Forwarded-Host $host;
 proxy_set_header X-Real-IP $remote_addr;               #保留代理之前的真实客户端ip
 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
nginx -s reopen                                       //重启
nginx -s reload                                       //重新加载配置文件
nginx -s quit                                         //优雅停止
nginx -s stop                                         //强制停止
killall nginx                                         //杀死所有nginx进程

nginx -t                                            //测试配置文件格式是否正确
nginx -t -c /etc/nginx/nginx.conf

systemctl stop/start/restart nginx                  //centos7的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://www.cnblogs.com/kazihuo/p/10755972.html
https://www.cnblogs.com/kazihuo/p/10755955.html
posted @ 2022-11-22 17:40  rbcd  阅读(71)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示