• nginx 代理域名到另外一个域名


    nginx 代理域名到另外一个域名

    server {
    		listen	80;
    		server_name  原域名;
    		location / {  
    			#return 301 http://新域名$request_uri;  # 重定向到新域名  
    			proxy_pass	http://新域名/;
    			proxy_redirect	http://原域名/ http://新域名/;
    		} 
    	}
    server {
            listen       80;
            server_name  新域名;
    		
    		# websocket代理
    		#location /webSocket/chat {
    		#	 proxy_pass  http://xxx.xxx.com/webSocket/chat;
            #    proxy_set_header Host $host;
            #     proxy_set_header Upgrade 'websocket';
            #     proxy_set_header Connection 'Upgrade';
    		#	 proxy_connect_timeout 4s;
    		#	 proxy_read_timeout 7200s; #两个小时
    		#	 proxy_send_timeout 12s;
    		#}
    		
            location / {		
    			proxy_pass	http://localhost:8080;
                root   html;
                index  index.html index.htm;
    			proxy_set_header   Host             $host;
    			proxy_set_header   X-Real-IP        $remote_addr;						
    			proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            }
    		
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    注意:用return,可以重定向到新域名的页面,但是请求方法是有问题的

    上述代码跑了几天后,不起作用了,又更新了其它代码
    server {
    		listen	80;
    		server_name  www.old.com;
    		
    		location / {  
    			proxy_pass http://www.new.com;
    			proxy_redirect off;
    		} 
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    上述代码又修改了一下

    server {
    		listen	80;
    		server_name  www.old.com;
    	
    		location / {  
    			proxy_set_header Host www.new.com;
    			return 307 http://www.new.com$request_uri;  # 重定向到新域名  
    			proxy_set_header X-Real-IP $remote_addr;
    			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    			#proxy_redirect	http://www.new.com http://www.yyhkjt.com;
    			#rewrite ^(.*)$ http://www.new.com$1 permanent;
    			#rewrite ^(.+)$ $scheme://www.new.com$1 redirect;
    			#return 301 $scheme://www.new.com$request_uri;
    			#proxy_pass	http://www.new.com; 
    		} 
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    postman请求会将post请求发送两次,并且第二次会转为get请求
    301:客户端在收到 301 响应后,会自动将 HTTP 请求转为 GET 请求,同时将请求地址修改为重定向后的地址
    307:对请求类型不做转换
    参考链接:https://huaweicloud.csdn.net/63561b80d3efff3090b5a828.html

  • 相关阅读:
    Java 实现MD5
    从0搭建Vue3组件库(七):使用 glup 打包组件库并实现按需加载
    ES状态查询相关API
    基于JAVA物业管理系统计算机毕业设计源码+数据库+lw文档+系统+部署
    C++初阶 Vector的介绍和使用
    Sketch mac98.3(ui设计矢量绘图)
    判断能否被3, 5, 7整除
    基于微信小程序的新冠疫苗预约系统 uinapp
    批量压缩图片软件-免费图片压缩后高清无损
    vue 微信登录
  • 原文地址:https://blog.csdn.net/weixin_44021888/article/details/133137195