nginx rewrite url

发布时间 2023-03-30 09:57:38作者: mvpbang

问题描述

url发请求转发url或重写url

#实现
1、http://sytest.x.cn/login-web/  -> ip:port/login-web/
2、http://sytest.x.cn/sclogin-web/  -> ip:port/login-web/
3、http://sytest.x.cn/sc/login-web/  -> ip:port/login-web/

问题解决

location  /login-web {
         proxy_pass http://login-web$request_uri;
         proxy_set_header   Host             $host;
         proxy_set_header   X-Real-IP        $remote_addr;
         proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

1、nginx

location  /login-web/ {
         proxy_pass http://login-web/login-web/;
         proxy_set_header   Host             $host;
         proxy_set_header   X-Real-IP        $remote_addr;
         proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

2、nginx

location  /sclogin-web/ {
         proxy_pass http://login-web/login-web/;
         proxy_set_header   Host             $host;
         proxy_set_header   X-Real-IP        $remote_addr;
         proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
3、nginx

location /sc/ {
        rewrite ^/sc/(.*)$ /$1 break;
        proxy_pass http://ip:5000/;
        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;
    }

location ~ ^/sc/(.*)$ {
        rewrite /sc/(.*) /$1 break;
        proxy_pass http://ip:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
 }