ruby-on-rails - 如何在 nginx 上部署 angular plus rails?
问题描述
我正在尝试配置我的 NGINX,我将 Angular 和 Rails 服务器托管在同一实例上,并且想知道如何让它们在同一服务器上一起工作。我已经
我的 NGINX 配置文件:
#rails config
server {
listen 3000;
server_name www.mysite.com;
root /var/www/mysite/public;
try_files $uri @app;
location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://mysite_prod;
proxy_intercept_errors on;
}
error_page 500 502 504 /500.html;
error_page 503 /503.html;
error_page 404 /404.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
#angular config
server {
listen 80;
listen [::]:80;
server_name www.mysite.com;
root /var/www/my_app/dist;
server_tokens off;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html =404;
}
}
在我的 rails 应用程序中,我已将 IP 添加到 Origins 文件中
配置/应用程序.rb
config.middleware.insert_before 0, "Rack::Cors" do
allow do
origins 'localhost:3000', 'www.mysite.com', 'localhost:4200',
resource '*',
headers: :any,
expose: ['access-token', 'expiry', 'token-type', 'uid', 'client'],
methods: [:get, :post, :options, :put]
end
end
我收到一个 cors 错误,前端无法与后端通信。错误 - '已被 CORS 策略阻止:'Access-Control-Allow-Origin' 标头有一个值'我如何让它工作?
解决方案
我认为您不应该运行两台服务器。只有 Rails 应用程序应该运行,Angular 应用程序应该只是 rails 应用程序提供的 index.html 文件。
构建 Angular 应用程序以进行分发,并将 Rails 的索引文件替换为 Angular 应用程序构建工件。一个你得到这个工作寻找选项,使你能够自动执行这个手动步骤。
推荐阅读
- networking - TCP和UDP中n的区别?
- hibernate - 休眠查询返回空值
- c# - 向 TCP 服务器发送数据
- java - Kafka Streams with processing.guarantee 设置为 EXACTLY_ONCE 问题
- java - build() 方法对 JAX-RS 中的响应有什么作用?
- reactjs - UI State 和 Application State 的边界
- javascript - 在 li 上添加叠加图像
- c - 为什么 Golang 比 ANSI C 更快,我该如何优化我的解决方案?
- c# - 为特定目录可视化 C# 创建一个 getter
- node.js - 你能像使用 nodejs 一样在 nginx 中使用`php artisan serve`吗?