繁体   English   中英

Nginx 为 Nodejs API 意外返回 404

[英]Nginx unexpectedly returns 404 for Nodejs API

我正在尝试在 Digital Ocean 上使用 Nginx 设置 VueJS 前端和 NodeJS api 后端服务器。 我在设置 Nginx 以使其正常工作时遇到问题。

更详细地说,我想要实现的目标:

  • 访问 http://ipaddressordomain/ - 返回 Vuejs 应用程序。
  • VueJS 应用程序需要从位于 http://ipaddressordomain/api 的后端发送和提取数据,Nodejs api 在同一台服务器上的端口 3333 上工作。

实际发生的情况:

  • 访问 http://ipaddressordomain/ - 毫无问题地返回 Vuejs 应用程序。 VueJS 似乎工作正常。
  • Vuejs 无法连接到后端 api。 通过浏览器或 Postman 访问 http://ipaddressordomain/api 也会返回 404。

配置

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/gabrielwillemann/fast-help/blob/master/nginx/sites-avaible-examples.md#for-mutiple-proxies

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM