nginx转发简单设置

更新时间 🔔🕙 2024年10月8日

环境:ubuntu14.04
nginx版本:1.4.6
最下面有centos6通过yum安装最新版本的方法
步骤:

1.安装nginx

参考网址:http://nginx.org/en/linux_packages.html

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
apt-get install nginx
apt-get install nginx
apt-get install nginx

2.设置转发策略

在目录【/etc/nginx/conf.d】中新建一个文件,只要是【XXX.conf】即可。
下面的代码表示,
浏览器访问【http://10.3.172.177/test/a】
实际访问【http://10.3.172.80:8080/a】

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
server{
listen 80;
server_name 10.3.172.177;
location /test/ {
if ($request_uri ~ "/test/(.+)") {
set $q $1;
}
proxy_pass http://10.3.172.80:8080/$q;
#session保持
proxy_cookie_path / /test/;
#302重定向保持
proxy_redirect http://10.3.172.80:8080/ http://10.3.172.177/test/;
#关闭日志输出
access_log off;
}
}
server{ listen 80; server_name 10.3.172.177; location /test/ { if ($request_uri ~ "/test/(.+)") { set $q $1; } proxy_pass http://10.3.172.80:8080/$q; #session保持 proxy_cookie_path / /test/; #302重定向保持 proxy_redirect http://10.3.172.80:8080/ http://10.3.172.177/test/; #关闭日志输出 access_log off; } }
server{
listen 80;
server_name 10.3.172.177;
location /test/ {
if ($request_uri ~ "/test/(.+)") {
set $q $1;
}
proxy_pass http://10.3.172.80:8080/$q;
#session保持
proxy_cookie_path / /test/;
#302重定向保持
proxy_redirect http://10.3.172.80:8080/ http://10.3.172.177/test/;
#关闭日志输出
access_log off;
}
}

3.修改完成之后,将nginx重新reload就可以了

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
nginx -s reload
# 或者是通过systemctl来操作
systemctl reload nginx
systemctl restart nginx
nginx -s reload # 或者是通过systemctl来操作 systemctl reload nginx systemctl restart nginx
nginx -s reload
# 或者是通过systemctl来操作
systemctl reload nginx
systemctl restart nginx

4.nginx中代理访问某个https的网站

浏览器访问【http://10.3.172.177/v3/ccb/kkd】
实际访问【https://api.xxxx.com/v3/ccb/kkd】

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
location /v3/ {
proxy_pass https://api.xxxx.com;
proxy_ssl_server_name on;
proxy_set_header Host api.xxxx.com;
proxy_set_header Connection '';
proxy_http_version 1.1;
chunked_transfer_encoding off;
proxy_buffering off;
proxy_cache off;
# proxy_set_header X-Forwarded-For $remote_addr;
# proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 20M; # 针对特定路径设置
}
location /v3/ { proxy_pass https://api.xxxx.com; proxy_ssl_server_name on; proxy_set_header Host api.xxxx.com; proxy_set_header Connection ''; proxy_http_version 1.1; chunked_transfer_encoding off; proxy_buffering off; proxy_cache off; # proxy_set_header X-Forwarded-For $remote_addr; # proxy_set_header X-Forwarded-Proto $scheme; client_max_body_size 20M; # 针对特定路径设置 }
    location /v3/ {
        proxy_pass  https://api.xxxx.com;
        proxy_ssl_server_name on;
        proxy_set_header Host api.xxxx.com;
        proxy_set_header Connection '';
        proxy_http_version 1.1;
        chunked_transfer_encoding off;
        proxy_buffering off;
        proxy_cache off;
#        proxy_set_header X-Forwarded-For $remote_addr;
#        proxy_set_header X-Forwarded-Proto $scheme;
        client_max_body_size 20M;  # 针对特定路径设置
    }
转载请备注引用地址:编程记忆 » nginx转发简单设置