Nginx

如何在nginx中接受域後的埠請求

  • May 19, 2021

我有一個子域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
}

如果你真的想graphql通過另一個埠訪問 URL,你需要有一個單獨的server塊:

server {
   listen 8080 ssl;

   ... ssl options ...

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

引用自:https://serverfault.com/questions/1064136