【问题标题】:Configuring Nginx as a proxy for a Vapor API将 Nginx 配置为 Vapor API 的代理
【发布时间】:2018-11-23 03:09:52
【问题描述】:

在尝试了几天后,我陷入了困境,我需要一些帮助。

我有一个运行良好的蒸汽 API。我创建了一个路由,可以在服务器上的浏览器中从http://localhost:8080/backend/returnA 访问它。它返回一些 JSON。

我遇到的困难是尝试将 Nginx 配置为服务器作为代理。谁能帮我理解http://localhost:8080/backend/returnA URL 是如何转换为可从 LAN 访问的有效 URL 的?

我很困惑,因为 Nginx.conf 要求输入根 URL,但我不知道该输入什么。如果我将其留空,则默认为 /usr/local/Cellar/nginx/1.15.6/ html/backend/returnA/index.html 这显然行不通。如果我将它设置为 Vapor 应用程序目录中的公用文件夹,这也不起作用。在这两种情况下,我都会收到“没有这样的文件或目录”。

我在网上找到了无数的 Nginx conf 设置,尝试添加代理位置,但没有任何效果。尝试http://172.16.1.25/backend/returnA/ 总是从 Nginx 服务器返回 404。

当 Nginx 不提供像 index.html 这样的静态文件,而是重新调整 JSON 时,如何将 Nginx 指向我的 Vapor 路由?

非常感谢任何帮助。

这是配置,已编辑以包含 Thanh 的代码,旧位置已注释掉:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

    http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local]       "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;

    server {
    server_name 172.16.1.25;
    listen       80  default_server;

    root /Users/localadmin/Developer/server/MedicapAPI/Public/;

    # location @proxy {
    #    proxy_pass http://127.0.0.1:8080;
    #    proxy_pass_header Server;
    #    proxy_set_header Host $host;
    #    proxy_set_header X-Real-IP $remote_addr;
    #    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #    proxy_pass_header Server;
    #    proxy_connect_timeout 3s;
    #    proxy_read_timeout 10s;
    # }

    location  / {
        proxy_ignore_client_abort on;
        proxy_pass http://localhost:8080/;
        proxy_redirect off;
    }    
    }
        include servers/*;
    }

【问题讨论】:

  • 你能分享一下你在这里试过的nginx.conf文件的内容吗?
  • 抱歉,已修复。
  • 请检查答案@rougement,刚刚编辑

标签: macos nginx vapor


【解决方案1】:

使用这个:

listen       80  default_server;
server_name 172.16.1.25; #ip address of server

它将捕获所有服务器块并且:

location  / {
    proxy_ignore_client_abort on;
    proxy_pass http://localhost:8080/;
    proxy_redirect off;
}  

在 8080 端口运行的应用程序将是 proxy_pass

【讨论】:

  • 嗨!我进行了您建议的更改,但现在出现 502 Bad Gateway 错误。控制台显示 [error] 14764#0: *1 no live upstreams while connection to upstream,客户端:172.16.1.10,服务器:localhost,请求:“GET /backend/returnA/ HTTP/1.1”,上游:“localhost/backend/returnA” ,主机:“172.16.1.25”
  • 改成server_name 172.16.1.25;再查看
  • 我仍然收到一个 502 错误,控制台说“在 SSL 握手到上游时 1 个对等端关闭了 SSL 握手中的连接”和“kevent() 报告连接时连接失败(61:连接被拒绝)到上游”。顺便说一句,谢谢你的帮助。我很感激。
  • proxy_pass https://localhost:8080/; 没有https,它只是http://,重新检查您的配置。
  • 我是这个网站的新手。你是说那个绿色的小支票吗?如果是这样,已经完成并再次感谢。我已经为此工作了好几天,但一无所获。
【解决方案2】:
server {
    #server_name  mysite.com;
    listen   80;

    error_log   /var/log/mysite.com_error.log warn;
    access_log  /var/log/mysite.com.ru_access.log;

    large_client_header_buffers 8 32k;
    client_max_body_size 10M;

    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;
    }

    # Give direct access to Public files of your app instead of using FileMiddleware
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml|html|mp4|pdf)$ {
        access_log        off;
        expires           30d;
        root /path/to/your/app/Public;
    }
}

这是我的工作示例。

对于生产,我建议您使用 SSL 证书,例如从 LetsEncrypt 中,将监听端口替换为 443 并在监听行之后添加以下配置行:

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/mysite.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mysite.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;

就是这样,现在您可以开始生产了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 2015-11-26
    • 2017-05-05
    • 2014-05-01
    • 2019-05-13
    相关资源
    最近更新 更多