node.js-端口号未隐藏在Nginx反向代理中(下一个JS服务器)

前端之家收集整理的这篇文章主要介绍了node.js-端口号未隐藏在Nginx反向代理中(下一个JS服务器) 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试通过create-next-app部署next-js应用程序,我有一个像这样的自定义快递服务器-

  1. const express = require('express')
  2. const next = require('next')
  3. const dev = process.env.NODE_ENV !== 'production'
  4. const nextApp = next({ dev })
  5. const handle = nextApp.getRequestHandler()
  6. const fs = require('fs')
  7. nextApp.prepare()
  8. .then(() => {
  9. const server = express ()
  10. let port = 3000;
  11. let options = {
  12. key: fs.readFileSync('some key..','utf-8'),cert: fs.readFileSync('some cert..',};
  13. server.get(
  14. ...
  15. )
  16. let app = https.createServer(options,server)
  17. .listen((port),function(){
  18. console.log("Express server listening on port " + port);
  19. });
  20. })
  21. .catch((ex) => {
  22. console.error(ex.stack)
  23. process.exit(1)
  24. })

当有人键入URL subdomain.maindomain.com时,我要将其部署为网站,因此我保存了两个这样的Nginx配置文件-

/ etc / Nginx / sites-available / default和/etc/Nginx/sites-available/subdomain.maindomain.com

默认文件包含此

  1. server {
  2. root /var/www/html;
  3. index index.html index.htm index.Nginx-debian.html;
  4. server_name maindomain.com www.maindomain.com;
  5. location / {
  6. # try_files $uri $uri/ =404;
  7. proxy_pass http://localhost:3000;
  8. proxy_http_version 1.1;
  9. proxy_set_header Upgrade $http_upgrade;
  10. proxy_set_header Connection 'upgrade';
  11. proxy_set_header Host $host;
  12. proxy_cache_bypass $http_upgrade;
  13. }
  14. listen [::]:443 ssl ipv6only=on; # managed by Certbot
  15. listen 443 ssl; # managed by Certbot
  16. ssl_certificate /etc/letsencrypt/live/maindomain.com/fullchain.pem;$
  17. ssl_certificate_key /etc/letsencrypt/live/maindomain.com/privkey.pe$
  18. include /etc/letsencrypt/options-ssl-Nginx.conf; # managed by Certbot
  19. ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
  20. }

而subdomain.maindomain.com文件如下所示

  1. server {
  2. if ($host = www.subdomain.maindomain.com) {
  3. return 301 https://$host$request_uri;
  4. } # managed by Certbot
  5. if ($host = subdomain.maindomain.com) {
  6. return 301 https://$host$request_uri;
  7. } # managed by Certbot
  8. listen 80;
  9. listen [::]:80;
  10. root /var/www/subdomain.maindomain.com/somecodefolder/;
  11. index index.html index.htm index.Nginx-debian.html;
  12. server_name subdomain.maindomain.com www.subdomain.maindomain.com;
  13. location / {
  14. proxy_pass http://localhost:3000;
  15. proxy_http_version 1.1;
  16. proxy_set_header Upgrade $http_upgrade;
  17. proxy_set_header Connection 'upgrade';
  18. proxy_set_header Host $host;
  19. proxy_cache_bypass $http_upgrade;
  20. # try_files $uri $uri/ =404;
  21. }

}

如果输入https://subdomain.maindomain.com:3000,一切正常,我看到我的网站正在运行.但是,当我键入https://subdomain.maindomain.com(无端口号)时,它什么也没有显示.当我只输入网址而不输入端口号时,如何获取所需的内容.我尝试了许多组合,但无法做到.有人请帮助我自2天以来一直在尝试.

最佳答案
尝试从

  1. proxy_pass http://localhost:3000;

进入

  1. proxy_pass http://127.0.0.1:3000;

猜你在找的Nginx相关文章