如何在NGINX中重定向一个URL

时间:2019-05-19 01:26:26  来源:igfitidea点击:

本教程将如何将请求重定向到NGINX web服务器的另一个域。
如果我们已经计划或更改了域名,并希望将通信从旧的域服务器重定向到新域,则需要这样做。
首先,编辑在NGINX中的域配置文件,并添加配置根据你的要求重定向。

$ vi /etc/nginx/sites-enabled/mydomain.com.conf

1。将所有请求重定向到特定的URL

这将重定向域上的所有传入请求到url http://anotherdomain.com/dir1/index.php,如下所配置。

server {
    listen 192.168.1.100:80;
    server_name mydomain.com;
    return 301 http://anotherdomain.com/dir1/index.php;
}

2。将所有请求重定向到其他域

这将用相应的请求url和查询字符串将域上的所有传入请求重定向到另一个域(http://anotherdomain.com/)。

server {
    listen 192.168.1.100:80;
    server_name mydomain.com;
    return 301 http://anotherdomain.com$request_uri;
}

3。使用特定协议重定向请求

这将用相应的请求url和查询字符串将域上的所有传入请求重定向到另一个域(http://anotherdomain.com/)。
此外,它将使用相同的协议重定向url。

server {
    listen 192.168.1.100:80;
    server_name mydomain.com;
    return 301 $scheme://anotherdomain.com$request_uri;
}

完成以上修改后,重启你的NGINX服务器来重新加载新添加的配置。