[英]Configuring Nginx Reverse Proxy for Node.js app
我有一个 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
时,我的应用程序不可用。 我应该检查什么? 我应该编辑或重新启动或激活什么? 谢谢!
您需要在 nginx 文件夹 ( /etc/nginx/sites-enabled/ ) 的 sites-enabled 文件夹中创建一个文件。
并添加在 80 后(或 443,如果您有 SSL 证书)上侦听请求的代理配置,并将其代理传递到http://127.0.0.1:3000/
然后尝试运行nginx -t重启 nginx 服务器。
希望对你有帮助!!
所以,我的经验可能有用。 我的 nginx.conf 的http
部分是
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/
创建了一个新链接sudo ln -s /etc/nginx/sites-available/myserver.config /etc/nginx/sites-enabled/
这样可行。 这不是什么新鲜事,但它证实了现有的指令确实可行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.