nginx 下如何在访问 url.com 时跳转到 url.com:9000(类似 301 跳转那种)

浏览:40日期:2022-07-17

问题描述

期望结果:

访问 http://url.com 自动将网址跳转到 http://url.com:9000,类似 301 跳转的那种,地址栏也跟着变。

由于 url.com 这个网址是不存在的,所以本地写了 host 指向 IP

在 nginx 里写了如下内容:

server { listen 80; server_name url.com; location / {proxy_pass http://url.com:9000; }}

但是测试 nginx 配置文件时提示:

$ sudo nginx -tnginx: [emerg] host not found in upstream 'seafile.sfdev.com' in /etc/nginx/sites-enabled/seafile.conf:5nginx: configuration file /etc/nginx/nginx.conf test failed

问题解答

回答1:

if ($host ~* url.com) {rewrite ^/(.*)$ http://url.com:9000/$1 permanent; }

或直接选用:

rewrite ^/(.*)$ http://url.com:9000/$1 permanent;

如果是直接跳转,不需要添加location字段,直接:

server { listen 80; server_name url.com; rewrite ^/(.*)$ http://url.com:9000/$1 permanent;}

而你配置文件中的:proxy_pass是将请求转发到代理服务器的,详情请看这里:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

回答2:

server { listen 80; server_name url.com; rewrite ^(.*) http://$server_name:9000$1 permanent;}server { listen 9000; server_name url.com; # other}回答3:

试试 rewrite ^ url redirect

回答4:

这你不用代理,重写url就好 rewrite ^/(.*)$ http://url.com:9000/$1 permanent;

相关文章: