我正在尝试在digital Ocean上用nginx设置一个VUEJS前端和一个nodejs api后端服务器。 在过去的几天里,我一直在努力设置NGINX以使其正常工作
更详细地说,我想要实现的是:
实际发生的情况:
配置:
NodeJS api路由可用路由应为端口3333上的http://myaddress/api:
router.route("/api/:sid").get(getUrlShorten);
router.route("/ai").post(postUrlShorten);
Nginx配置:
Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name lien.to;
location / {
root /var/www/html/liento-fe/dist;
}
location /api/ {
proxy_pass http://localhost:3333/;
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
防火墙(UFW):
Status: active
To Action From
-- ------ ----
Nginx HTTP ALLOW Anywhere
OpenSSH ALLOW Anywhere
Nginx HTTP (v6) ALLOW Anywhere (v6)
OpenSSH (v6) ALLOW Anywhere (v6)
另一个侧面问题:在DO上,代理地址应该是localhost还是DO服务器的实际IP?
有人能给我指个正确的方向吗? 我是不是遗漏了什么? 如果需要任何额外的信息,请让我知道,我会提供。
谢谢。
尝试删除try_files$URI$URI/=404;
:
location /api/ {
proxy_pass http://localhost:3333/;
}
这对我很有效:https://github.com/gabrielwilleman/fast-Help/blob/master/nginx/sites-avaible-examples.md#for-Mutiple-proxies