Nginx 意外地为 Nodejs API 返回 404

Vic*_*tor 3 nginx node.js digital-ocean vue.js

我正在尝试在 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);
Run Code Online (Sandbox Code Playgroud)

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
        #
         
Run Code Online (Sandbox Code Playgroud)

防火墙(UFW):

Status: active

To                         Action      From
--                         ------      ----
Nginx HTTP                 ALLOW       Anywhere                  
OpenSSH                    ALLOW       Anywhere                  
Nginx HTTP (v6)            ALLOW       Anywhere (v6)             
OpenSSH (v6)               ALLOW       Anywhere (v6)             
Run Code Online (Sandbox Code Playgroud)

还有一个附带问题:在 DO 上,位置代理地址应该是 localhost 还是 DO 服务器的实际 IP?

接下来我可以尝试什么?我有什么遗漏的吗?

Gab*_*ann 5

尝试删除try_files $uri $uri/ =404;

location /api/ {
  proxy_pass http://localhost:3333/;
}
Run Code Online (Sandbox Code Playgroud)

这对我有用: https: //github.com/gabrielwillemann/fast-help/blob/master/nginx/sites-avaible-examples.md#for-mutiple-proxies