我有一個 Express 應用程式,服務器監聽 3000 埠。它托管在 VPS、Ubuntu 上。當我{VPS server ip}:3000在瀏覽器中打開時,一切正常。但現在我想訪問應用程式example.com,那是我的域名。我已經安裝了 Nginx,啟用了 UFW。我編輯了檔案/etc/nginx/nginx.conf,現在它的內容是
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
listen 80;
server_name example.com[-->my real domain here<--];
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://[-->my real server ip here<--]:3000;
}
}
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
nginx -t說語法沒問題。然后systemctl restart nginx,它是活躍的。當我example.com在瀏覽器中打開時,我的應用程式不可用。我應該檢查什么?我應該編輯或重新啟動或激活什么?謝謝!
uj5u.com熱心網友回復:
您需要在 nginx 檔案夾 ( /etc/nginx/sites-enabled/ ) 的 sites-enabled 檔案夾中創建一個檔案。
并添加代理配置監聽post 80(或443,如果你有SSL證書)和proxypass它到http://127.0.0.1:3000/
然后嘗試運行nginx -t重啟 nginx 服務器。
希望對你有幫助!!
uj5u.com熱心網友回復:
所以,我的經驗可能有用。我的httpnginx.conf 部分是
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
顯然,線條
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
讓所有作業。/sites-available 檔案夾中的單個檔案名為“myserver.config”,其完整內容為
#The Nginx server instance
server{
listen 80;
server_name example.com;
location / {
proxy_pass http://myIP:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
我從 /sites-enabled 中洗掉了我在澄清程序中創建的所有符號鏈接,并使用命令創建了一個新鏈接
sudo ln -s /etc/nginx/sites-available/myserver.config /etc/nginx/sites-enabled/
這樣可行。這不是什么新鮮事,但它證實了現有的指令確實可行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/475974.html
