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

期望结果:

访问 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 -t
nginx: [emerg] host not found in upstream "seafile.sfdev.com" in /etc/nginx/sites-enabled/seafile.conf:5
nginx: configuration file /etc/nginx/nginx.conf test failed
阅读 5k
4 个回答
    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

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

试试 rewrite ^ url redirect

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题