Nginx 出乎意料 returns 404 for Nodejs API
Nginx unexpectedly returns 404 for Nodejs API
我正在尝试在 Digital Ocean 上使用 Nginx 设置一个 VueJS 前端和一个 NodeJS api 后端服务器。我一直在设置 Nginx 以使其正常工作时遇到问题。
更详细地说明我正在努力实现的目标:
- 正在访问 http://ipaddressordomain/ - returns Vuejs 应用程序。
- VueJS 应用程序需要从位于 http:///ipaddressordomain/api 的后端发送和拉取数据,Nodejs api 在同一台服务器上的端口 3333 上工作。
实际发生了什么:
- 访问 http://ipaddressordomain/ - returns Vuejs 应用程序没有问题。 VueJS 似乎工作正常。
- Vuejs 无法连接到后端 api。还通过浏览器或 Postman 访问 http:///ipaddressordomain/api returns 404。
配置:
NodeJS api routes 可用的路由应该是 http://myaddress/api 端口 3333:
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上定位代理地址应该是本地主机还是DO服务器的实际IP?
接下来我可以尝试什么?有什么我想念的吗?
尝试删除 try_files $uri $uri/ =404;
:
location /api/ {
proxy_pass http://localhost:3333/;
}
我正在尝试在 Digital Ocean 上使用 Nginx 设置一个 VueJS 前端和一个 NodeJS api 后端服务器。我一直在设置 Nginx 以使其正常工作时遇到问题。
更详细地说明我正在努力实现的目标:
- 正在访问 http://ipaddressordomain/ - returns Vuejs 应用程序。
- VueJS 应用程序需要从位于 http:///ipaddressordomain/api 的后端发送和拉取数据,Nodejs api 在同一台服务器上的端口 3333 上工作。
实际发生了什么:
- 访问 http://ipaddressordomain/ - returns Vuejs 应用程序没有问题。 VueJS 似乎工作正常。
- Vuejs 无法连接到后端 api。还通过浏览器或 Postman 访问 http:///ipaddressordomain/api returns 404。
配置:
NodeJS api routes 可用的路由应该是 http://myaddress/api 端口 3333:
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上定位代理地址应该是本地主机还是DO服务器的实际IP?
接下来我可以尝试什么?有什么我想念的吗?
尝试删除 try_files $uri $uri/ =404;
:
location /api/ {
proxy_pass http://localhost:3333/;
}