Skip to content

[nginx]域名映射虚机,带端口自动跳转解决 #22

Description

@YutHelloWorld

案例

现有内网虚机 10.88.88.88 部署前端静态资源,启用8000端口。同时http://abcd.com 根据/hyyy映射到虚机 http://10.88.88.88:8000。
先贴上nginx配置:

server {
     listen       8000;
     server_name  localhost;
   
    location /hyyy {
        root   /app/hyyy; #虚机用户目录
        index  index.html index.htm;
        try_files $uri $uri/ /hyyy/index.html;
    }
...
}

虚机文件目录

|-app
  |- hyyy
      |- hyyy
          |- index.html

启动nginx, 测试结果:

10.88.88.88:8000/hyyy 自动跳转到了 10.88.88.88:8000/hyyy/ 。虚机表现是我们所期望的。
但是wget域名,结果发生错误

域名301重定向跳转后多出了个8000端口。导致域名无法正常访问虚机。

这里的坑

如果server带了listen port , 自动跳转会带上这个port, 万一前面还有一层反向代理,就出现意外了。

解决办法

明白了是啥坑,解决办法就是去掉带上带port
只要在配置中添加port_in_redirect off ;
配置如下:

server {
     listen       8000;
     server_name  localhost;
     port_in_redirect off ;
    location /hyyy {
        root   /app/hyyy; #虚机用户目录
        index  index.html index.htm;
        try_files $uri $uri/ /hyyy/index.html;
    }
...
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions