无法从Web服务器上的教程访问我的Vapor应用程序

时间:2019-11-29 16:57:52

标签: nginx vapor

我想在服务器上部署Vapor应用程序以将其用作iOS应用程序的后端。

我对这个话题还很陌生。我之前所做的唯一一件事就是在同一服务器上部署Django后端。我重建服务器以设置Vapor后端。

首先,我想尽可能地部署Vapor应用程序。 我遵循了本教程(简短): https://medium.com/@ankitank/deploy-a-basic-vapor-app-with-nginx-and-supervisor-1ef303320726

我按照步骤操作,没有出现错误。

问题是,当我尝试像本教程中那样呼叫[IP]/hello时,我得到502 Bad Gateway作为答案。

Nginx给我这个错误:

connect() failed (111: Connection refused) while connecting to upstream, client: [IP], server: _, request: "GET /hello HTTP/1.1", upstream: "http://127.0.0.1:8080/hello", host: "[IP]"

希望您能对此有所帮助。 :)


更新1:

我将配置更改为此:

server {
    listen  80;
    listen  [::]:80;
    server_name  [DOMAIN];

    error_log   /var/log/[DOMAIN]_error.log warn;
    access_log  /var/log/[DOMAIN]_access.log;

    add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";

    large_client_header_buffers 8 32k;

    location / {
        # redirect all traffic to localhost:8080;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_pass http://127.0.0.1:8080/;
        proxy_redirect off;
        proxy_read_timeout 86400;

        # enables WS support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        # prevents 502 bad gateway error
        proxy_buffers 8 32k;
        proxy_buffer_size 64k;

        reset_timedout_connection on;

        tcp_nodelay on;

        client_max_body_size 10m;
    }

    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml|html|mp4)$ {
      access_log        off;
      expires           30d;
      root /home/[AppName]/Public;
    }
}

不幸的是我还是得到这个:

2019/12/01 14:48:04 [error] 6801#6801: *1 connect() failed (111: Connection refused) while connecting to upstream, client: [IP], server: [DOMAIN], request: "GET /hello HTTP/1.1", upstream: "http://127.0.0.1:8080/hello", host: [DOMAIN]

更新2:

错误与以下行有关: proxy_pass http://127.0.0.1:8080/;

我不得不将其更改为: proxy_pass http://localhost:8080/;

好像本地主机不一样。

现在,我可以通过“ vapor run”运行该应用程序,并且可以访问它。 :)

非常感谢@imike的所有帮助!

2 个答案:

答案 0 :(得分:2)

您可以尝试使用SSL和websockets支持的100%工厂生产配置

server {
    listen  443;
    listen  [::]:443;
    server_name  mydomain.com;

    error_log   /var/log/mydomain.com_error.log warn;
    access_log  /var/log/mydomain.com_access.log;

    ssl on;
    ssl_session_cache    shared:SSL:10m;
    ssl_session_timeout  10m;
    ssl_prefer_server_ciphers on;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
    ssl_ciphers 'HIGH:!aNULL:!MD5:!kEDH';
    add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";
    ssl_stapling on;
    ssl_stapling_verify on;

    large_client_header_buffers 8 32k;

    location / {
        # redirect all traffic to localhost:8080;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_pass http://127.0.0.1:8080/;
        proxy_redirect off;
        proxy_read_timeout 86400;

        # enables WS support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        # prevents 502 bad gateway error
        proxy_buffers 8 32k;
        proxy_buffer_size 64k;

        reset_timedout_connection on;

        tcp_nodelay on;

        client_max_body_size 10m;
    }

    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml|html|mp4)$ {
      access_log        off;
      expires           30d;
      root /apps/myApp/Public;
    }
}

在配置末尾,您会看到公共文件夹nginx中的静态文件将直接返回,而无需运行Vapor应用程序。

在config.swift文件中,您仅应在不使用nginx的情况下测试应用的macOS上使用FileMiddleware,因为该中间件的运行速度很慢,因此建议您将其放入编译器检查中

#if os(macOS)
middlewares.use(FileMiddleware.self) // Serves files from `Public/` directory
#endif

答案 1 :(得分:-1)

错误与配置文件中的这一行有关:
library(dplyr) library(purrr) library(tibble) library(stringr) map_df(ht, enframe, .id = 'year') %>% group_by(year = str_remove(year, "^\\d+-"), name) %>% summarise(value = max(value)) %>% split(.$year)

我不得不将其更改为此:
proxy_pass http://127.0.0.1:8080/;

好像本地主机不一样。

现在,我可以通过“ vapor run”运行该应用程序,并且可以访问它。 :)

非常感谢@imike的所有帮助!他解决了!