将Nginx配置为Vapor API的代理

时间:2018-11-23 03:09:52

标签: macos nginx vapor

经过几天的努力后,我陷入了困境,我可以寻求帮助。

我有一个工作良好的蒸气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指向我的蒸气路线?

非常感谢您的帮助。

这里是配置,已编辑为包含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/*;
    }

2 个答案:

答案 0 :(得分:0)

使用此:

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

它将捕获所有服务器blokck和:

location  / {

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

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

答案 1 :(得分:0)

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;

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