Rails Passenger Glyphicon CORS Cloudfront NGINX问题 [英] Rails Passenger Glyphicon CORS Cloudfront NGINX Issue

查看:151
本文介绍了Rails Passenger Glyphicon CORS Cloudfront NGINX问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我意识到,stackoverflow是与关于CORS Nginx,Cloudfront和Heroku的这些问题产生的,但由于某种原因,我无法让它工作。我一直在回答这个问题的答案:

如何使用rails,nginx和passenger配置`Access-Control-Allow-Origin`?



然而,我似乎无法弄清楚在哪里放置自定义代码块:


  config / nginx.conf.erb 

接下来,编辑配置文件config / nginx.conf。 erb通过找到一个
块,如下所示:

  location @static_asset {
gzip_static on;
过期max;
add_header Cache-Control public;
add_header ETag;
} ...并添加两条访问控制行:




 > location @static_asset {
> gzip_static on;
> expires max;
> add_header Cache-Control public;
> add_header ETag;
> add_header Access-Control-Allow-Origin *;
> add_header访问控制请求方法*;
> } 而已。由于两者之间的config.assets不同,这将在生产中工作,但不在开发中。

在我的Nginx配置中。我正在使用Phusion Passenger 5.0.23。 Nginx配置没有在我从乘客那里获得的位置@static_asset块。它确实有一个自定义配置部分,但没有为我工作。我的字形继续显示为盒子如何使它起作用?
我也试过这个
Phusion Passenger + Heroku + Cloudfront: CORS配置



当前配置文件

  ## ################################################## ###################### 
#Passenger Standalone建立在与Nginx的
#Passenger相同的技术上,因此任何配置选项由Nginx的Passenger
#支持,也可以应用于Passenger Standalone。您可以通过直接编辑由
#Passenger Standalone使用的Nginx配置模板来完成
#this操作。

#此文件是原始模板。请勿直接编辑此文件。
#取而代之,复制这个文件并将'--nginx-config-template`
#参数传递给Passenger Standalone。

#详细了解如何在以下位置使用Nginx配置模板:
#https://www.phusionpassenger.com/library/config/standalone/intro.html#nginx-configuration-template

#***注意***
#如果您自定义模板文件,请确保您密切关注
#原始模板文件并合并所有更改。新的Phusion Passenger
#功能可能需要更改模板文件。
############################################## ################

<%= include_passenger_internal_template('global.erb')%>

worker_processes 1;
事件{
worker_connections 1024;
}

http {
<%= include_passenger_internal_template('http.erb',4)%>



###开始您自己的配置选项###
#这是放置您自己的配置
#选项的好地方。请注意,您的选项不得与$ Passenger已经设置的
#冲突。
#详情请参阅:
#https://www.phusionpassenger.com/library/config/standalone/intro.html#nginx-configuration-template

### END你自己的配置选项###

default_type application / octet-stream;
types_hash_max_size 2048;
server_names_hash_bucket_size 64;
client_max_body_size 1024m;
access_log off;
keepalive_timeout 60;
underscores_in_headers on;
gzip on;
gzip_comp_level 3;
gzip_min_length 150;
gzip_proxied any;
gzip_types text / plain text / css text / json text / javascript
application / javascript application / x-javascript application / json
application / rss + xml application / vnd.ms-fontobject application / x-font-ttf
application / xml font / opentype image / svg + xml text / xml;

<%if if app_finder.multi_mode? %GT;
#批量部署模式的默认服务器条目。
服务器{
<%= include_passenger_internal_template('mass_deployment_default_server.erb',12)%>
}
<%end%>

<%for app in @apps%>
server {
<%= include_passenger_internal_template('server.erb',8,true,binding)%>
<%= include_passenger_internal_template('rails_asset_pipeline.erb',8,false)%>

###开始您自己的配置选项###
#这是放置您自己的配置
#选项的好地方。请注意,您的选项不得与$ Passenger已经设置的
#冲突。
#了解更多信息:
#https://www.phusionpassenger.com/library/config/standalone/intro.html#nginx-configuration-template

#CloudFront修复
location / public / * {
gzip_static on;
过期max;
add_header Cache-Control public;
add_header ETag;
add_header Access-Control-Allow-Origin *;
add_header访问控制请求方法*;
}


###结束您自己的配置选项###
}
passenger_pre_start<%= listen_url(app)%>;
<%end%>
}


解决方案

这个配置适用于我:

  #################### ################################################## #### 
#Passenger Standalone基于相同的技术,为Nginx提供
#Passenger支持,因此Nginx的Passenger
#支持的配置选项也可以应用于Passenger Standalone 。您可以通过直接编辑由
#Passenger Standalone使用的Nginx配置模板来完成
#this操作。

#此文件是原始模板。请勿直接编辑此文件。
#取而代之,复制这个文件并将'--nginx-config-template`
#参数传递给Passenger Standalone。

#详细了解如何在以下位置使用Nginx配置模板:
#https://www.phusionpassenger.com/library/config/standalone/intro.html#nginx-configuration-template

#***注意***
#如果您自定义模板文件,请确保您密切关注
#原始模板文件并合并所有更改。新的Phusion Passenger
#功能可能需要更改模板文件。
############################################## ################

<%= include_passenger_internal_template('global.erb')%>

worker_processes 1;
事件{
worker_connections 4096;
}

http {
<%= include_passenger_internal_template('http.erb',4)%>

###开始您自己的配置选项###
#这是放置您自己的配置
#选项的好地方。请注意,您的选项不得与$ Passenger已经设置的
#冲突。
#详情请参阅:
#https://www.phusionpassenger.com/library/config/standalone/intro.html#nginx-configuration-template

### END你自己的配置选项###

default_type application / octet-stream;
types_hash_max_size 2048;
server_names_hash_bucket_size 64;
client_max_body_size 1024m;
access_log off;
keepalive_timeout 60;
underscores_in_headers on;
gzip on;
gzip_comp_level 3;
gzip_min_length 150;
gzip_proxied any;
gzip_types text / plain text / css text / json text / javascript
application / javascript application / x-javascript application / json
application / rss + xml application / vnd.ms-fontobject application / x-font-ttf
application / xml font / opentype image / svg + xml text / xml;

<%if if app_finder.multi_mode? %GT;
#批量部署模式的默认服务器条目。
服务器{
<%= include_passenger_internal_template('mass_deployment_default_server.erb',12)%>
}
<%end%>

<%for app in @apps%>
server {
<%= include_passenger_internal_template('server.erb',8,true,binding)%>
<%#<%= include_passenger_internal_template('rails_asset_pipeline.erb',8,false)%1> %GT;

###开始您自己的配置选项###
#这是放置您自己的配置
#选项的好地方。请注意,您的选项不得与$ Passenger已经设置的
#冲突。
#详细了解:
#https://www.phusionpassenger.com/library/config/standalone/intro.html#nginx-configuration-template
#Rails资产管道支持。
location〜^ / assets /.+-([0-9a-f] {32} | [0-9a-f] {64})\ .. +{
error_page 490 = @static_asset;
error_page 491 = @dynamic_request;
recursive_error_pages on;

if(-f $ request_filename){
return 490;
}
if(!-f $ request_filename){
return 491;
}
}
位置@static_asset {
gzip_static on;
过期max;
add_header Cache-Control public;
add_header ETag;
if($ http_origin〜*((https?:\ / \ / [^ \ /] * \.herokuapp\.com(:[0-9] +)?))){
add_header'Access-Control-Allow-Origin'$ http_origin;
add_header'Access-Control-Allow-Credentials''true';
add_header'访问控制 - 允许 - 方法''GET,HEAD';
add_header'Access-Control-Allow-Headers''接受,授权,缓存控制,内容类型,DNT,If-Modified-Since,Keep-Alive,Origin,用户代理,X-Mx-ReqToken中,X-请求-随着';
}
}
位置@dynamic_request {
passenger_enabled on;
}

###结束您自己的配置选项###
}
passenger_pre_start<%= listen_url(app)%>;
<%end%>

<%= include_passenger_internal_template('footer.erb',4)%>
}


So I am aware that stackoverflow is wrought with these questions about CORS Nginx, Cloudfront and Heroku but for some reason I can't get it to work. I have been following this question's answer:

How do I configure `Access-Control-Allow-Origin` with rails, nginx and passenger?

However I can't seem to figure out where to put the custom code block:

config/nginx.conf.erb

Next, edit the configuration file config/nginx.conf.erb by finding a block that looks as follows:

location @static_asset {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
    add_header ETag "";
} ...and add the two Access-Control lines:

to

>     location @static_asset {
>         gzip_static on;
>         expires max;
>         add_header Cache-Control public;
>         add_header ETag "";
>         add_header Access-Control-Allow-Origin *;
>         add_header Access-Control-Request-Method *;
>     } That's it. This will work in production, but not in development, due to config.assets differences between the two.

in my Nginx configuration. I am using Phusion Passenger 5.0.23. The Nginx config doesnt have a location @static_asset block in the one that I pulled from passenger. It does have a section for custom configurations but that hasnt worked for me. My glyphicons continue to show up as boxes How do I get it to work? I have also tried this Phusion Passenger + Heroku + Cloudfront: CORS Configuration

Current Config File

    ##########################################################################
#  Passenger Standalone is built on the same technology that powers
#  Passenger for Nginx, so any configuration option supported by Passenger
#  for Nginx can be applied to Passenger Standalone as well. You can do
#  this by direct editing the Nginx configuration template that is used by
#  Passenger Standalone.
#
#  This file is the original template. DO NOT EDIT THIS FILE DIRECTLY.
#  Instead, make a copy of this file and pass the `--nginx-config-template`
#  parameter to Passenger Standalone.
#
#  Learn more about using the Nginx configuration template at:
#  https://www.phusionpassenger.com/library/config/standalone/intro.html#nginx-configuration-template
#
#  *** NOTE ***
#  If you customize the template file, make sure you keep an eye on the
#  original template file and merge any changes. New Phusion Passenger
#  features may require changes to the template file.
##############################################################

<%= include_passenger_internal_template('global.erb') %>

worker_processes 1;
events {
    worker_connections 1024;
}

http {
    <%= include_passenger_internal_template('http.erb', 4) %>



    ### BEGIN your own configuration options ###
    # This is a good place to put your own config
    # options. Note that your options must not
    # conflict with the ones Passenger already sets.
    # Learn more at:
    # https://www.phusionpassenger.com/library/config/standalone/intro.html#nginx-configuration-template

    ### END your own configuration options ###

    default_type application/octet-stream;
    types_hash_max_size 2048;
    server_names_hash_bucket_size 64;
    client_max_body_size 1024m;
    access_log off;
    keepalive_timeout 60;
    underscores_in_headers on;
    gzip on;
    gzip_comp_level 3;
    gzip_min_length 150;
    gzip_proxied any;
    gzip_types text/plain text/css text/json text/javascript
        application/javascript application/x-javascript application/json
        application/rss+xml application/vnd.ms-fontobject application/x-font-ttf
        application/xml font/opentype image/svg+xml text/xml;

    <% if @app_finder.multi_mode? %>
        # Default server entry for mass deployment mode.
        server {
            <%= include_passenger_internal_template('mass_deployment_default_server.erb', 12) %>
        }
    <% end %>

    <% for app in @apps %>
    server {
        <%= include_passenger_internal_template('server.erb', 8, true, binding) %>
        <%= include_passenger_internal_template('rails_asset_pipeline.erb', 8, false) %>

        ### BEGIN your own configuration options ###
        # This is a good place to put your own config
        # options. Note that your options must not
        # conflict with the ones Passenger already sets.
        # Learn more at:
        # https://www.phusionpassenger.com/library/config/standalone/intro.html#nginx-configuration-template

        # CloudFront fix 
        location /public/* {
            gzip_static on;
            expires max;
            add_header Cache-Control public;
            add_header ETag "";
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Request-Method *;
        }


        ### END your own configuration options ###
    }
    passenger_pre_start <%= listen_url(app) %>;
    <% end %>
}

解决方案

I had the same issue and this config worked for me:

    ##########################################################################
#  Passenger Standalone is built on the same technology that powers
#  Passenger for Nginx, so any configuration option supported by Passenger
#  for Nginx can be applied to Passenger Standalone as well. You can do
#  this by direct editing the Nginx configuration template that is used by
#  Passenger Standalone.
#
#  This file is the original template. DO NOT EDIT THIS FILE DIRECTLY.
#  Instead, make a copy of this file and pass the `--nginx-config-template`
#  parameter to Passenger Standalone.
#
#  Learn more about using the Nginx configuration template at:
#  https://www.phusionpassenger.com/library/config/standalone/intro.html#nginx-configuration-template
#
#  *** NOTE ***
#  If you customize the template file, make sure you keep an eye on the
#  original template file and merge any changes. New Phusion Passenger
#  features may require changes to the template file.
##############################################################

<%= include_passenger_internal_template('global.erb') %>

worker_processes 1;
events {
    worker_connections 4096;
}

http {
    <%= include_passenger_internal_template('http.erb', 4) %>

    ### BEGIN your own configuration options ###
    # This is a good place to put your own config
    # options. Note that your options must not
    # conflict with the ones Passenger already sets.
    # Learn more at:
    # https://www.phusionpassenger.com/library/config/standalone/intro.html#nginx-configuration-template

    ### END your own configuration options ###

    default_type application/octet-stream;
    types_hash_max_size 2048;
    server_names_hash_bucket_size 64;
    client_max_body_size 1024m;
    access_log off;
    keepalive_timeout 60;
    underscores_in_headers on;
    gzip on;
    gzip_comp_level 3;
    gzip_min_length 150;
    gzip_proxied any;
    gzip_types text/plain text/css text/json text/javascript
        application/javascript application/x-javascript application/json
        application/rss+xml application/vnd.ms-fontobject application/x-font-ttf
        application/xml font/opentype image/svg+xml text/xml;

    <% if @app_finder.multi_mode? %>
        # Default server entry for mass deployment mode.
        server {
            <%= include_passenger_internal_template('mass_deployment_default_server.erb', 12) %>
        }
    <% end %>

    <% for app in @apps %>
    server {
        <%= include_passenger_internal_template('server.erb', 8, true, binding) %>
        <%# <%= include_passenger_internal_template('rails_asset_pipeline.erb', 8, false) %1> %>

        ### BEGIN your own configuration options ###
        # This is a good place to put your own config
        # options. Note that your options must not
        # conflict with the ones Passenger already sets.
        # Learn more at:
        # https://www.phusionpassenger.com/library/config/standalone/intro.html#nginx-configuration-template
        # Rails asset pipeline support.
        location ~ "^/assets/.+-([0-9a-f]{32}|[0-9a-f]{64})\..+" {
            error_page 490 = @static_asset;
            error_page 491 = @dynamic_request;
            recursive_error_pages on;

            if (-f $request_filename) {
                return 490;
            }
            if (!-f $request_filename) {
                return 491;
            }
        }
        location @static_asset {
            gzip_static on;
            expires max;
            add_header Cache-Control public;
            add_header ETag "";
            if ($http_origin ~* ((https?:\/\/[^\/]*\.herokuapp\.com(:[0-9]+)?))) {
                add_header 'Access-Control-Allow-Origin' "$http_origin";
                add_header 'Access-Control-Allow-Credentials' 'true';
                add_header 'Access-Control-Allow-Methods' 'GET, HEAD';
                add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With';
            }
        }
        location @dynamic_request {
            passenger_enabled on;
        }

        ### END your own configuration options ###
    }
    passenger_pre_start <%= listen_url(app) %>;
    <% end %>

    <%= include_passenger_internal_template('footer.erb', 4) %>
}

这篇关于Rails Passenger Glyphicon CORS Cloudfront NGINX问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆