我对 nginx 还很陌生,很难让最简单的东西在 digitalocean ubuntu 16.04 服务器上运行。我有一个在 pm2 后面运行的 node.js 应用程序,它可以很好地处理对 的请求mydomain.com:5002/endpoint
。但是,我无法让它在 /app 等单独位置上运行,不断出现 404。这是/etc/nginx/sites-available/default
:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
server_name mydomain.com;
location /app/ {
proxy_pass http://localhost:5002;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
答案1
如果您正在运行 pm2 example.com:5002/endpoint
,那么您的位置块需要说明:
location /app { proxy_pass http://localhost:5002/endpoint; }
你应该能够通过以下 URL 访问你的 nodejs 应用程序:http://example.com/app
如需进一步参考,您可以参考:https://devtidbits.com/2015/12/08/nginx-as-a-reverse-proxy-to-apache-tomcat/它处理相同的设置,只是端口不同。