【问题标题】:How to deploy React front end + Rails api backend with nginx, capistrano and passenger如何使用 nginx、capistrano 和乘客部署 React 前端 + Rails api 后端
【发布时间】:2018-04-27 20:11:50
【问题描述】:

我正在部署一个使用create-react-app 创建的 React 前端和一个 Rails API 作为后端的应用程序。我对配置 Web 服务器和代理了解不多,所以我不确定如何让它在生产中工作。我正在将应用程序部署到 Amazon EC2 实例上的 Ubuntu。 Nginx 是网络服务器。我需要对其进行配置,以便 Nginx 提供来自 client/build 目录的静态文件,然后向 /api 发出的任何请求转到运行在端口 3001 上的 Rails 应用程序。现在 Nginx 正在为 index.html 提供服务,而 Javascript 是运行正常,但对/api 的请求没有到达正确的位置。关于如何配置 Nginx 来做到这一点的任何想法?这是我的/etc/nginx/sites-enabled/default 文件:

server {
       listen 80 default_server;
       listen [::]:80 default_server;
       server_name mydomain.com;
       passenger_enabled on;
       rails_env    staging;
       root         /home/ubuntu/app-name/current/client/build;
       index index.html;

       location /api {
          proxy_pass http://127.0.0.1:3001;
       }
}

我错过了什么?如何让 Rails 应用程序在端口 3001 上运行,并将所有对 /api 的请求都发送到那里?我是否需要在此配置中使用单独的 server 块?

【问题讨论】:

    标签: ruby-on-rails reactjs nginx deployment http-proxy


    【解决方案1】:

    我不知道您是否已经解决了您的问题以及此解决方案是否适用于您,但我认为它可能对其他搜索此问题的人有用。

    我使用 Puma 作为应用服务器来运行我的 rails 5 api。

    这是我的开发环境的配置文件:

    upstream app {
    # Path to Puma SOCK file, here is where you make the connection to your application server
    server unix:/path/to/your/puma.sock fail_timeout=0;
    }
    server {
    listen 80;
    server_name mydomain.com;
    
    # this is where my react-app is located
    root /var/www/development/ram/public/;
    index index.html index.htm;
    
    # Serve the static content (React app)
    location / {
        try_files $uri /index.html =404;
    }
    
    location /api {
        # Insert your public app path
        root /your/rails-app/path/public;
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }
    
    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
    }
    

    所以比较一下,我认为你的问题可以通过添加一个指向你的 rails api 公共目录的根指令来解决

    我希望这能给你一些关于如何配置你的提示

    【讨论】:

      【解决方案2】:

      这是我的工作 nginx 配置

        map $http_upgrade $connection_upgrade {
          default upgrade;
          ''      close;
      }
      server {
        listen 80 default_server;
        root /home/deploy/www/sublime/current/public;
        index index.html;
        server_name domain.com;
        access_log /home/deploy/www/sublime/logs/access.log;
        error_log /home/deploy/www/sublime/logs/errors.log;
        server_name localhost;
        passenger_enabled on;
        passenger_app_env production;
      location ~* ^.+\.(jpeg|gif|png|jpg) {
                  proxy_pass http://127.0.0.1:3000;
                  proxy_http_version 1.1;
        }
        location /api {
                  # Insert your public app path
                  proxy_pass http://127.0.0.1:3000;
                  proxy_http_version 1.1;
                  proxy_set_header Host $http_host;
                  proxy_set_header Upgrade $http_upgrade;
                  proxy_set_header Connection $connection_upgrade;
                  proxy_buffering off;
        }
        location / {
                    # First attempt to serve request as file, then
                    # as directory, then fall back to displaying a 404.
                    try_files $uri /index.html;
        }
      }
      

      您可以使用这个 repos 查看 rails 配置 https://github.com/Eth3rnit3/rails-react

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-09-07
        • 2022-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多