如何在nginx中接受域后端口的请求

How to accept request with port after domain in nginx

我有一个子域 https://test.shop.com,我是 运行 Nginx 服务器,它工作正常。但是我必须接受 https://test.shop.com:8080/graphql/ 的请求并重定向到 http://127.0.0.1:8000 到同一台机器。我已经添加了这个块

    location /graphql/ {
       proxy_pass http://127.0.0.1:8000;
    }

但是当我尝试从浏览器访问 https://test.shop.com:8080/graphql/ 时,它显示 无法访问此站点 似乎与 dns 有关。虽然我可以访问 https://test.shop.com/graphql/ 并且它工作正常。

我的整个配置文件是

    server {
    server_name test.shop.com;
    root /var/www/html/test;
    index index.html;
    location / {
        try_files $uri $uri/ /index.html?$args;
      }
        # dashboard app
    location /dashboard/ {
        try_files $uri $uri/ /dashboard/index.html?$args;
    }

    location /graphql/ {
       proxy_pass http://127.0.0.1:8000;
    }


    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/test.shop.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/test.shop.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
    server {
    if ($host = test.shop.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    listen [::]:80;
    server_name test.shop.com;
    return 404; # managed by Certbot
}

您必须创建新的虚拟主机并监听该虚拟主机的 8080 端口。

server {
  listen 8080 ssl;
  server_name test.shop.com;
  root /var/www/html/test;
  index index.html;

  location /graphql/ {
    proxy_pass http://127.0.0.1:8000;
  }


  ssl_certificate /etc/letsencrypt/live/test.shop.com/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/test.shop.com/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}