简体   繁体   English

Nginx 为 Nodejs API 意外返回 404

[英]Nginx unexpectedly returns 404 for Nodejs API

I'm trying to setup an VueJS frontend and a NodeJS api backend server with Nginx on Digital Ocean.我正在尝试在 Digital Ocean 上使用 Nginx 设置 VueJS 前端和 NodeJS api 后端服务器。 I have been having trouble setting up Nginx to work correctly.我在设置 Nginx 以使其正常工作时遇到问题。

In more details what I'm trying to achieve:更详细地说,我想要实现的目标:

  • Accessing http://ipaddressordomain/ - returns Vuejs app.访问 http://ipaddressordomain/ - 返回 Vuejs 应用程序。
  • VueJS apps needs to send and pull data from backend which would be at http://ipaddressordomain/api, the Nodejs api works on port 3333 on the same server. VueJS 应用程序需要从位于 http://ipaddressordomain/api 的后端发送和提取数据,Nodejs api 在同一台服务器上的端口 3333 上工作。

What actually happens:实际发生的情况:

  • Accessing http://ipaddressordomain/ - returns Vuejs app without issue.访问 http://ipaddressordomain/ - 毫无问题地返回 Vuejs 应用程序。 VueJS seems to work ok. VueJS 似乎工作正常。
  • Vuejs cannot connect to backend api. Vuejs 无法连接到后端 api。 Also accessing http://ipaddressordomain/api returns 404 via browser or Postman.通过浏览器或 Postman 访问 http://ipaddressordomain/api 也会返回 404。

CONFIGURATION :配置

NodeJS api routes available routes should be http://myaddress/api on port 3333: NodeJS api 路由可用路由应该是端口 3333 上的 http://myaddress/api:

router.route("/api/:sid").get(getUrlShorten);
router.route("/ai").post(postUrlShorten);

Nginx configuration: 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
        #
         

Firewall (UFW):防火墙(UFW):

Status: active

To                         Action      From
--                         ------      ----
Nginx HTTP                 ALLOW       Anywhere                  
OpenSSH                    ALLOW       Anywhere                  
Nginx HTTP (v6)            ALLOW       Anywhere (v6)             
OpenSSH (v6)               ALLOW       Anywhere (v6)             

Also side-question: on DO the location proxy address should be localhost or the actual IP of the DO server?还有一个问题:在 DO 上,位置代理地址应该是 localhost 还是 DO 服务器的实际 IP?

What can I try next?接下来我可以尝试什么? Is there something I'm missing?有什么我想念的吗?

Try to remove the try_files $uri $uri/ =404;尝试删除try_files $uri $uri/ =404; :

location /api/ {
  proxy_pass http://localhost:3333/;
}

This works for me: https://github.com/gabrielwillemann/fast-help/blob/master/nginx/sites-avaible-examples.md#for-mutiple-proxies这对我有用: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