端口号未隐藏在Nginx反向代理中(下一个JS服务器) [英] Port numbers not hiding in nginx reverse proxy (next js server)

查看:90
本文介绍了端口号未隐藏在Nginx反向代理中(下一个JS服务器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to deploy a next-js app by create-next-app, I have a custom express server like this -

const express = require('express')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const nextApp = next({ dev })
const handle = nextApp.getRequestHandler()

const fs = require('fs')

nextApp.prepare()
.then(() => {
    const server = express ()


    let port = 3000;

    let options = {
        key: fs.readFileSync('some key..', 'utf-8'),
        cert: fs.readFileSync('some cert..', 'utf-8'),
    };


    server.get(
        ...
    )


    let app = https.createServer(options, server)
    .listen((port), function(){
    console.log("Express server listening on port " + port);
    });

})
.catch((ex) => {
    console.error(ex.stack)
    process.exit(1)
})

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

I want to deploy this as the website when someone types the URL subdomain.maindomain.com so I saved two nginx configuration files like this -

/etc/nginx/sites-available/default /etc/nginx/sites-available/subdomain.maindomain.com

默认文件包含此

server {

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;

    server_name maindomain.com www.maindomain.com;

    location / {
            # try_files $uri $uri/ =404;
            proxy_pass http://localhost:3000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/maindomain.com/fullchain.pem;$
    ssl_certificate_key /etc/letsencrypt/live/maindomain.com/privkey.pe$
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

和subdomain.maindomain.com文件看起来像这样

and the subdomain.maindomain.com file looks like this

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


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


    listen 80;
    listen [::]:80;

    root /var/www/subdomain.maindomain.com/somecodefolder/;
    index index.html index.htm index.nginx-debian.html;

    server_name subdomain.maindomain.com www.subdomain.maindomain.com;


    location / {

        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
       # try_files $uri $uri/ =404;
    }

}

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

if I'm typing https://subdomain.maindomain.com:3000, everything works fine, I see my website running. But when I type https://subdomain.maindomain.com (without the port number) it shows nothing. How can I get the content I want when I type just the url without the port number. I have tried many combinations, but could'nt do. someone please help i've been trying since 2 days.

推荐答案

尝试使用其他应用程序,以验证您的应用程序中是否存在错误.

Try with other applications in order to validate if something is wrong in your application.

将nginx配置为使用域,而不是复杂的端口.只需添加https配置,但主要配置将相同.

Configure nginx to use domain instead ports are not complex. Just add https configurations but the main configurations will be the same.

  • npm安装
  • 节点main_domain.js
  • 节点subdomain.js
  • 检查网站是否正常运行

  • 将以下行添加到/etc/hosts中.这将帮助我们在没有企业网络托管公司注册的情况下使用域.

127.0.0.1 maindomain.com
127.0.0.1 subdomain.maindomain.com

  • 在/etc/nginx/conf.d中创建一个名为 maindomain.com.conf 的文件,或者使用.conf
    • Create a file in /etc/nginx/conf.d called maindomain.com.conf or whatever you want but with .conf
    • server {
          listen 80;
          server_name maindomain.com;
          
          location / {
              proxy_pass http://localhost:3000/;
          }
      }
      

      • 在/etc/nginx/conf.d中创建一个名为 conf.d/subdomain.maindomain.com.conf 的文件,或者使用.conf
        • Create a file in /etc/nginx/conf.d called conf.d/subdomain.maindomain.com.conf or whatever you want but with .conf
        • server {
              listen 80;
              server_name subdomain.maindomain.com;
              
              location / {
                  proxy_pass http://localhost:3001/;
              }
          }
          

          • 重新启动nginx
          • service nginx restart
            

            • 现在,您可以使用域代替ip:port
            • 这篇关于端口号未隐藏在Nginx反向代理中(下一个JS服务器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆