您的位置:首页 > 运维架构 > 反向代理

Nginx使用301反向代理的各种配置

2020-06-07 04:48 986 查看

    相信Nginx的各种配置,大家都不陌生。使用各类配置,可以实现各种域名、IP按需求的实现。本文介绍几种关于Nginx-301重定向的配置方法。关于301重定向和302重定向,各位可自行查阅相关文档。

     进入正题:

     1、实现需求将http://www.123456.net 和 http://abc.com.cn全部重定向到http://www.pleasure.com域名上,直接上配置:

      

server {
        listen       50000;
        server_name  www.pleasure.com;
        charset utf-8 

 location / {
            #root   html;
            #index  index.html index.htm;

     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             ...........................................

        }

2、在原有server{}的配置下面,再新增2个server配置

server {
           server_name  www.123456.net;
    
           return 301 http://www.pleasure.com:50000;
    }

server  {
        server_name  abc.com.cn;

 return 301 http://www.pleasure.com:50000;
    }

3、保存配置,重新加载nginx(reload命令,不必restart)

4、需求2,将pleasure.com/index.html、pleasure.com、www.pleasure.com/index.html三个域名,全部重定向到www.pleasure.com域名

(1)、对于带有index.html的扩展名域名,可使用location定义,并重定向

server {
        listen       50000;
        server_name  pleasure.com;
        charset utf-8 

 location = /index.html { return 301 $scheme://www.pleasure.com:50000/;}

 location / {
            #root   html;
            #index  index.html index.htm;

     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             ...........................................

    }

  (2)、不带index.html的域名(pleasure.com),不能使用301重定向实现,具体实现方式如下:

   

server {
        listen       50000
        server_name  www.pleasure.com;-----直接把域名改为目标域名
        

        charset utf-8
        location = /index.html { return 301 $scheme://www.pleasure.com:50000/;}

        location / {
            #root   html;
            #index  index.html index.htm;

}

在原有server以外,再新建一个server组,做如下配置

server  {
        server_name  pleasure.com;
        rewrite ^(.*)$ http://www.pleasure.com$1 permanent;

    }

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: