首页 > 解决方案 > 将nginx 8080端口转发到80

问题描述

我有一个带有多个 nginx 实例的服务器

其中一个运行在 8080

server {
    listen       8080;
    server_name  myip v2.example.com www.v2.example.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        proxy_pass  http://v2.example.com:8080/;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    
}

但我想使用 example.com 进入站点,而不是 example.com:8080 问题是在端口 80 上运行了另一台服务器,我尝试使用代理通行证修复它,但它似乎不起作用

我该如何解决?

标签: nginx

解决方案


用 重定向return。您可以将以下内容放在一个文件中:

server {
    listen 80;
    listen [::]:80;

    server_name v2.example.com www.v2.example.com;

    return 301 http://$host$request_uri:8080;
}

server {
    listen       8080;
    server_name  myip v2.example.com www.v2.example.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }    
}

推荐阅读