2016-02-23 35 views
6

Vì vậy, tôi biết rằng stackoverflow được rèn với những câu hỏi về CORS Nginx, Cloudfront và Heroku nhưng vì lý do nào đó tôi không thể làm cho nó hoạt động. Tôi đã được sau câu trả lời của câu hỏi này:Rails Hành khách Glyphicon CORS Cloudfront NGINX Phát hành

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

Tuy nhiên tôi dường như không thể tìm ra nơi để đặt các khối mã tùy chỉnh:

config/nginx.conf.erb 

Tiếp theo, chỉnh sửa file cấu hình cấu hình /nginx.conf.erb bằng cách tìm một khối trông như sau:

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

để

>  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. 

trong cấu hình Nginx tôi. Tôi đang sử dụng Phusion Passenger 5.0.23. Cấu hình Nginx không có vị trí chặn @static_asset trong khối mà tôi đã kéo từ hành khách. Nó có một phần cho cấu hình tùy chỉnh nhưng nó không hoạt động cho tôi. Glyphicons của tôi tiếp tục hiển thị như hộp Làm thế nào để tôi làm cho nó hoạt động? Tôi cũng đã cố gắng này Phusion Passenger + Heroku + Cloudfront: CORS Configuration

hiện 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 %> 
} 
+0

là @static_asset thiết lập? không ai biết những gì đặt này? – portforwardpodcast

+0

@portforwardpodcast Tôi đã hardcoded nó vào lúc này nó vẫn không hoạt động. – ChiefRockaChris

Trả lời

1

tôi đã cùng một vấn đề và cấu hình này làm việc cho tôi:

########################################################################## 
# 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) %> 
} 
Các vấn đề liên quan