簡體   English   中英

端口號未隱藏在 nginx 反向代理中(下一個 js 服務器)

[英]Port numbers not hiding in nginx reverse proxy (next js server)

我正在嘗試通過 create-next-app 部署 next-js 應用程序,我有一個像這樣的自定義快速服務器 -

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 AND /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 時,如何獲得我想要的內容。 我嘗試了很多組合,但都做不到。 有人請幫助我自 2 天以來一直在嘗試。

嘗試從

proxy_pass http://localhost:3000;

進入

proxy_pass http://127.0.0.1:3000;

嘗試使用其他應用程序以驗證您的應用程序是否有問題。

配置 nginx 使用域而不是端口並不復雜。 只需添加 https 配置,但主要配置將相同。

腳步

  • 安裝
  • 節點 main_domain.js
  • 節點子域.js
  • 檢查網絡是否正常工作:

本地主機

  • 將以下行添加到您的 /etc/hosts.conf 中。 這將幫助我們在沒有企業網絡托管公司注冊的情況下使用域。

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

域

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM