目 录CONTENT

文章目录

Nginx隐藏式跳转反向代理

不争
2024-01-02 / 0 评论 / 0 点赞 / 12 阅读 / 3038 字

Nginx隐藏式跳转反向代理

Nginx的隐藏式跳转可以实现将请求跳转到另一个网站的页面,并且浏览器中URL保持不变。Nginx配置中需要使用rewrite规则。

1.根目录代理香港服务器的80前端端口

  location / {
    proxy_pass http://154.204.60.38:80;
}

2./api/路径代理到后端端口8000

  • 重点在rewrite 后的 ^/api/(.*)$ 会获取到/api/路径后面的url 然后$1重写url
location /api/
{
    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;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-Proto https;
    add_header X-Cache $upstream_cache_status;
    rewrite  ^/api/(.*)$ /$1 break;
    proxy_pass http://154.204.60.38:8000;
}

3.完整配置

server {
  listen 80;
  listen [::]:80;
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  ssl_certificate /usr/local/nginx/conf/ssl/tzz.zinzin.cc.crt;
  ssl_certificate_key /usr/local/nginx/conf/ssl/tzz.zinzin.cc.key;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
  ssl_prefer_server_ciphers on;
  ssl_session_timeout 10m;
  ssl_session_cache builtin:1000 shared:SSL:10m;
  ssl_buffer_size 1400;
  add_header Strict-Transport-Security max-age=15768000;
  ssl_stapling on;
  ssl_stapling_verify on;
  server_name tzz.zinzin.cc;
  access_log /data/wwwlogs/tzz.zinzin.cc_nginx.log combined;
  index index.html index.htm index.php;
  root /data/wwwroot/tzz.zinzin.cc;

  include /usr/local/nginx/conf/rewrite/none.conf;
  #error_page 404 /404.html;
  #error_page 502 /502.html;

  location / {

    proxy_pass http://154.204.60.38:80;
}

location /api/
{
    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;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-Proto https;
    add_header X-Cache $upstream_cache_status;
    rewrite  ^/api/(.*)$ /$1 break;
    proxy_pass http://154.204.60.38:8000;
}

}

配置成功后:https://tzz.zinzin.cc/api/user/getInfo 会自动转成https://tzz.zinzin.cc/user/getInfo 完成访问

Nginx Proxy Manger操作

1.关于NPM的安装在推荐一个nginx管理工具讲过

image-20230912212643827

2.设置api

image-20230912212834622

3.证书泛域名

image-20230912212851748

0

评论区