【问题标题】:Port numbers not hiding in nginx reverse proxy (next js server)端口号未隐藏在 nginx 反向代理中(下一个 js 服务器)
【发布时间】:2019-06-01 09:47:24
【问题描述】:

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

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 配置文件 -

/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 文件看起来像这样

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(没有端口号)时,它什么也没有显示。当我只输入没有端口号的 url 时,如何获得我想要的内容。我尝试了很多组合,但做不到。有人请帮助我两天以来一直在尝试。

【问题讨论】:

  • 您是否尝试打开主机上的 443 端口? sudo netstat -anp | grep listen -i的结果是什么
  • 您正在尝试通过https:// 访问,因此您希望nginx 像您使用listen 443 配置的那样侦听端口443。您的 nginx 似乎没有在该端口上侦听。
  • 你在配置 listen 443 ssl; 时做了,为什么端口没有监听我不能告诉你,你是在正确的主机上以 sudo 运行命令吗?
  • 尝试查看具有server_name maindomain.com www.maindomain.com;的vhost的nginx访问/错误日志
  • 那么看来443端口是开放的,尝试用curlcurl -vv -L -H 'Host: maindomain.com ' https://localhosttelnet localhost 443验证一下

标签: node.js nginx proxy devops next.js


【解决方案1】:

尝试改变

proxy_pass http://localhost:3000;

进入

proxy_pass http://127.0.0.1:3000;

【讨论】:

  • 更新了我的评论
  • 我改了,没运气
【解决方案2】:

尝试使用其他应用程序,以验证您的应用程序是否有问题。

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

步骤

  • npm 安装
  • node main_domain.js
  • node subdomain.js
  • 检查网络是否正常工作:

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

127.0.0.1 maindomain.com
127.0.0.1 subdomain.maindomain.com
  • 在 /etc/nginx/conf.d 中创建一个名为 ma​​indomain.com.conf 的文件或任何您想要但使用 .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

server {
    listen 80;
    server_name subdomain.maindomain.com;
    
    location / {
        proxy_pass http://localhost:3001/;
    }
}
  • 重启nginx

service nginx restart
  • 现在,您可以使用域来代替 ip:port

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    相关资源
    最近更新 更多