[英]Hiding Node app port numbers on one domain with nginx reverse proxy
[英]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 配置,但主要配置將相同。
127.0.0.1 maindomain.com
127.0.0.1 subdomain.maindomain.com
server {
listen 80;
server_name maindomain.com;
location / {
proxy_pass http://localhost:3000/;
}
}
server {
listen 80;
server_name subdomain.maindomain.com;
location / {
proxy_pass http://localhost:3001/;
}
}
service nginx restart
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.