提问者:小点点

需要NGINX 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。 此外,通过浏览器或邮递员访问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?

有人能给我指个正确的方向吗? 我是不是遗漏了什么? 如果需要任何额外的信息,请让我知道,我会提供。

谢谢。


共1个答案

匿名用户

尝试删除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