端口号不隐藏在nginx反向代理(下一个js服务器)

bxgwgixi  于 2023-04-11  发布在  Nginx
关注(0)|答案(2)|浏览(161)

我正在尝试通过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)
})

我想把它部署为网站,当有人输入网址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(没有端口号)它什么也没显示.我怎么能得到我想要的内容时,我只输入网址没有端口号.我已经尝试了许多组合,但'nt做.有人请帮助我一直在努力,因为2天.

jmo0nnb3

jmo0nnb31#

尝试使用其他应用程序,以验证应用程序中是否存在错误。
使用domain代替ports的nginx配置并不复杂。

步骤

  • npm install
  • 节点main_domain.js
  • node subdomain.js
  • 检查腹板是否正常工作:

  • 将以下行添加到您的/etc/hosts中。这将帮助我们使用域名,而无需在godaddy,namecheap等中支付域名。
127.0.0.1 maindomain.com
127.0.0.1 subdomain.maindomain.com
  • 在/etc/nginx/conf.d中创建一个名为maindomain.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

7vux5j2d

7vux5j2d2#

试着从

proxy_pass http://localhost:3000;

走进

proxy_pass http://127.0.0.1:3000;

相关问题