AWS前端后端通信

时间:2018-03-31 10:24:44

标签: reactjs symfony amazon-web-services routing reverse-proxy

我在端口8000(本地)上运行ASW Symfony上的2个应用程序并且响应3000(本地)但通过端口80上的TCP可访问,通过侦听nginx服务器中的端口80实现了重定向。

server {
    listen 80;
    server_name example.info  www.example.info;
    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
     }
}
server {
    listen 8000;
    server_name example.info  www.example.info;
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
     }

}

我试图侦听和重定向两个端口,但没有成功。 在服务器中,可以使用curl http://127.0.0.1:8000访问Symfony应用程序 在我的反应应用程序的外部我发送api请求asw.external.ip(123.123.123.123:800),但我得到超时。我怎么能从外面访问我的后端?

1 个答案:

答案 0 :(得分:1)

AWS ElasticBeanstalk - 将代理服务器配置到后端

您也可以将此配置文件用于Aws Ec2。

/etc/nginx/conf.d/proxy.conf

  upstream nodejs {
    server 127.0.0.1:5000;
    keepalive 256;
  }

  server {
    listen 8080;

    access_log  /var/log/nginx/access.log  main;

    location / {
        proxy_pass  http://nodejs;
        proxy_set_header   Connection "";
        proxy_http_version 1.1;
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    gzip on;
    gzip_comp_level 4;
    gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ## Optional configuration if you want to allow AWS 
    ## to cache your static files
    location /static {
        alias /var/app/current/static;
    }
  }

编辑 - 为Symfony配置Nginx

server {
    listen             8080;
    server_name        sf2testproject.dev;

    root /home/maurits/public_html/web;

    location / {
        # try to serve file directly, fallback to rewrite
        try_files $uri @rewriteapp;
    }

    location @rewriteapp {
        # rewrite all to app.php
        rewrite ^(.*)$ /app.php/$1 last;
    }

    location ~ ^/(app|app_dev|config)\.php(/|$) {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }
}

其中:

  • listen是您的应用程序与世界通信的端口。
  • fastcgi_pass是用于将交互式程序与Web服务器连接的二进制协议

参考文献: