我正在使用create-react-app
部署一个带有React前端的应用程序,并使用Rails API作为后端。我不太了解配置Web服务器和代理,所以我不确定如何让它在生产中工作。我正在Amazon EC2实例上将应用程序部署到Ubuntu。 Nginx是Web服务器。我需要配置它以便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
块?
答案 0 :(得分: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公共目录的root指令来解决
我希望这可以为您提供有关如何配置您的一些提示
答案 1 :(得分:0)
这是我正在工作的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;
}
}
您可以使用此存储库查看rails配置 https://github.com/Eth3rnit3/rails-react