NGINX හි ප්‍රවේශ-පාලනය-ඉඩ දෙන්න-සම්භවය එකතු කරන්නේ කෙසේද?


166

මගේ ප්‍රධාන වසමේ මගේ උප ඩොමේනයෙන් වෙබ්-අකුරු භාවිතා කිරීමට හැකි වන පරිදි මම ප්‍රවේශ-පාලක-ඉඩ දෙන්න-ප්‍රභව ශීර්ෂය සකසන්නේ කෙසේද?


සටහන්:

HTML5BP සේවාදායකයේ වින්‍යාස ව්‍යාපෘති https://github.com/h5bp/server-configs හි බොහෝ HTTP සේවාදායකයන් සඳහා මේ සහ වෙනත් ශීර්ෂයන් ඔබට උදාහරණ වනු ඇත.


4
ah අවසානයේ පිළිතුරු ස්ථානය සොයා ගත්තේය / {add_header Access-Control-Allow-Origin "*"; }
ක්‍රිස් මැකී

Answers:


194

Nginx http://wiki.nginx.org/NginxHttpHeadersModule සමඟ සම්පාදනය කළ යුතුය (උබුන්ටු සහ වෙනත් ලිනක්ස් ඩිස්ට්‍රෝස් වල පෙරනිමිය). එවිට ඔබට මෙය කළ හැකිය

location ~* \.(eot|ttf|woff|woff2)$ {
    add_header Access-Control-Allow-Origin *;
}

ඔබට එකම විසඳුම apache මත ක්‍රියාත්මක කිරීමට අවශ්‍ය නම් මෙම උපදෙස් අනුගමනය කරන්න: stackoverflow.com/questions/11616306/…
camilo_u

7
එම මොඩියුලය පෙරනිමියෙන් සම්පාදනය කර ඇති බව පෙනේ (අවම වශයෙන් උබුන්ටු මත).
ස්ටීව් බෙනට්

1
ඇමසන් ලිනක්ස් රෙපෝ හි පෙරනිමියෙන් සම්පාදනය කරන ලදි
රොස්

1
අප මෙම ස්ථානය නියම කළ යුත්තේ කුමන ගොනුවේ සහ ස්ථානයේද?
සුමිත් අරෝරා

1
එය මට වැඩ කරන්නේ නැත. Nginx 1.10.0, උබුන්ටු 16.04
Omid Amraei

43

වඩාත් යාවත්කාලීන පිළිතුරක්:

#
# Wide-open CORS config for nginx
#
location / {
     if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        #
        # Om nom nom cookies
        #
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        #
        # Custom headers and headers various browsers *should* be OK with but aren't
        #
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        #
        # Tell client that this pre-flight info is valid for 20 days
        #
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
     }
     if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     }
     if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     }
}

මූලාශ්‍රය: https://michielkalkman.com/snippets/nginx-cors-open-configuration.html

Access-Control-Expose-Headersඔබේ අභිරුචි සහ / හෝ 'සරල නොවන' ශීර්ෂයන් අජැක්ස් ඉල්ලීම් වලට නිරාවරණය කිරීම සඳහා (ප්‍රවේශ-පාලන-ඉඩ දෙන්න-ශීර්ෂයන් මෙන්) එකතු කිරීමටද ඔබට අවශ්‍ය විය හැකිය .

Access-Control-Expose-Headers (optional) - The XMLHttpRequest 2 object has a 
getResponseHeader() method that returns the value of a particular response 
header. During a CORS request, the getResponseHeader() method can only access 
simple response headers. Simple response headers are defined as follows:

    Cache-Control
    Content-Language
    Content-Type
    Expires
    Last-Modified
    Pragma
 If you want clients to be able to access other headers, you have to use the
 Access-Control-Expose-Headers header. The value of this header is a comma-
 delimited list of response headers you want to expose to the client.

- http://www.html5rocks.com/en/tutorials/cors/

වෙනත් වෙබ් සේවාදායකයන් සඳහා වින්‍යාස කිරීම http://enable-cors.org/server.html


1
සෑම ස්ථානයක් සඳහාම මෙම රේඛා නැවත නොකිරීමට කිසියම් ක්‍රමයක් තිබේද? අපට එය සේවාදායකය}} වාරණය යටතේ තැබිය හැකිද?
geyws

@geoyws (without මට දැනුම් දීමක් නොලැබුණි); ඔබට එය ස්ථානයට ඉහළින් තැබිය හැකිය, එය හොඳයි :)
ක්‍රිස් මැකී

access-control-
expose

3
කරුණාකර ifnginx හි භාවිතා කිරීමෙන් වළකින්න - නිල අත්පොත පවා එය අධෛර්යමත් කරයි .
සමස්ථ

1
alwaysසියල්ලටම විකල්පයක් එක් කිරීම ප්‍රයෝජනවත් බව එක් කිරීමට මම කැමැත්තෙමි, add_headerඑවිට 200 නොවන ප්‍රතිචාර සඳහා ශීර්ෂයන්ද එක් කරනු ලැබේ. : විකිපීඩියා 1.7.5 සිට nginx.org/en/docs/http/ngx_http_headers_module.html
Mitar

14

GET | POST සඳහා සමහර අනුපිටපත් මග හැරෙන මා ලියූ ලිපිය මෙන්න. එය ඔබට Nginx හි CORS සමඟ යා යුතුය.

nginx ප්‍රවේශ පාලනය සම්භවයට ඉඩ දෙයි

තනතුරේ නියැදි ස්නිපටය මෙන්න:

server {
  listen        80;
  server_name   api.test.com;


  location / {

    # Simple requests
    if ($request_method ~* "(GET|POST)") {
      add_header "Access-Control-Allow-Origin"  *;
    }

    # Preflighted requests
    if ($request_method = OPTIONS ) {
      add_header "Access-Control-Allow-Origin"  *;
      add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS, HEAD";
      add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
      return 200;
    }

    ....
    # Handle request
    ....
  }
}

3
SF ප්‍රතිපත්තියට අනුව ඔබට තොරතුරු තනතුරට පිටපත් කිරීමට අවශ්‍යය. වෙබ් අඩවි ඕනෑම වේලාවක අතුරුදහන් විය හැකි අතර එය තොරතුරු නැතිවීමකි.
ටිම්

2
වලංගු ලක්ෂ්‍යය @tim, කේතය ඇතුළත් කිරීම සඳහා යාවත්කාලීන කරන ලදි
gansbrest

1
204 No contentවඩාත් සුදුසු යැයි පෙනෙන පරිදි තත්ව කේතය භාවිතා කිරීම සලකා බලන්න .
ස්ලාවා ෆොමින් II

8

පළමුවෙන්ම, හෙල්වින්ස් පිළිතුර මා වෙනුවෙන් වැඩ කරන බව මට කියන්නට ඉඩ දෙන්න:

location ~* \.(eot|ttf|woff|woff2)$ {
    add_header Access-Control-Allow-Origin *;
}

කෙසේ වෙතත්, මෙම ප්‍රශ්නයට වෙනම පිළිතුරකින් පිළිතුරු දීමට මම තීරණය කර ඇති අතර, විසඳුම සෙවීම සඳහා තවත් පැය දහයක් පමණ යෙදවීමෙන් පසුව මෙම විසඳුම ක්‍රියාත්මක කිරීමට මට හැකි විය.

Nginx විසින් පෙරනිමියෙන් කිසිදු (නිවැරදි) අකුරු MIME වර්ග නිර්වචනය නොකරන බව පෙනේ. මෙම නිබන්ධනය අනුගමනය කිරීමෙන් මට පහත සඳහන් දෑ එකතු කළ හැකි බව මට පෙනී ගියේය :

application/x-font-ttf           ttc ttf;
application/x-font-otf           otf;
application/font-woff            woff;
application/font-woff2           woff2;
application/vnd.ms-fontobject    eot;

මගේ etc/nginx/mime.typesගොනුවට. ඉහත සඳහන් විසඳුම පසුව ක්‍රියාත්මක විය.


2
මම සාමාන්‍යයෙන් මිනිසුන්ට M5 වර්ගයේ ගොනුව H5BP github.com/h5bp/server-configs-nginx/blob/master/mime.types හි පරීක්ෂා කර බැලීමට යොමු කරමි :)
ක්‍රිස් මැකී

4

Nginx හි සාම්ප්‍රදායික add_header විධානය 4xx ප්‍රතිචාර සමඟ ක්‍රියා නොකරයි. අපට තවමත් අභිරුචි ශීර්ෂයන් එක් කිරීමට අවශ්‍ය බැවින්, වැඩි_සෙට්_හෙඩර්ස් විධානය භාවිතා කිරීමට හැකිවන පරිදි අපි ngx_headers_more මොඩියුලය ස්ථාපනය කළ යුතු අතර එය 4xx ප්‍රතිචාර සමඟද ක්‍රියා කරයි.

sudo apt-get install nginx-extras

එවිට භාවිතා more_set_headers මෙම nginx.conf ගොනුවේ, මම පහත මගේ ආදර්ශ අලවා

server {
    listen 80;
    server_name example-site.com;
    root "/home/vagrant/projects/example-site/public";

    index index.html index.htm index.php;

    charset utf-8;

    more_set_headers 'Access-Control-Allow-Origin: $http_origin';
    more_set_headers 'Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE, HEAD';
    more_set_headers 'Access-Control-Allow-Credentials: true';
    more_set_headers 'Access-Control-Allow-Headers: Origin,Content-Type,Accept,Authorization';

    location / {
        if ($request_method = 'OPTIONS') {
            more_set_headers 'Access-Control-Allow-Origin: $http_origin';
            more_set_headers 'Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE, HEAD';
            more_set_headers 'Access-Control-Max-Age: 1728000';
            more_set_headers 'Access-Control-Allow-Credentials: true';
            more_set_headers 'Access-Control-Allow-Headers: Origin,Content-Type,Accept,Authorization';
            more_set_headers 'Content-Type: text/plain; charset=UTF-8';
            more_set_headers 'Content-Length: 0';
            return 204;
        }
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/example-site.com-error.log error;

    sendfile off;

    client_max_body_size 100m;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

    location ~ /\.ht {
        deny all;
    }
}

3

සමහර අවස්ථාවලදී ඔබ සියලු HTTP ප්‍රතිචාර කේත ආවරණය කිරීම සඳහා add_headerවිධානයන් භාවිතා කළ alwaysයුතුය .

location / {
    add_header 'Access-Control-Allow-Origin' '*' always;
}

සිට ප්රලේඛනය :

සෑම විටම පරාමිතිය නියම කර ඇත්නම් (1.7.5), ප්‍රතිචාර කේතය නොසලකා ශීර්ෂ ක්ෂේත්‍රය එකතු වේ.

ප්‍රතිචාර කේතය 200, 201 (1.3.10), 204, 206, 301, 302, 303, 304, 307 (1.1.16, 1.0.13), හෝ 308 (1.13) ට සමාන වන පරිදි නිශ්චිත ක්ෂේත්‍රය ප්‍රතිචාර ශීර්ෂයකට එක් කරයි. .0). පරාමිති අගයට විචල්යයන් අඩංගු විය හැකිය.


0

මගේ නඩුවේදී, රේල්ස් 5 භාවිතා කරමින්, වැඩ කරන එකම විසඳුම rack-corsමැණික් එකතු කිරීමයි . එසේ වැනි:

/ Gemfile තුළ

# Gemfile
gem 'rack-cors'

config / initializers / cors.rb හි

# config/initializers/cors.rb
Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins 'localhost:4200'
    resource '*',
      headers: :any,
      methods: %i(get post put patch delete options head)
  end
end

මූලාශ්‍රය: https://til.hashrocket.com/posts/4d7f12b213-rails-5-api-and-cors


ස්ථිතික ලිපිගොනු සේවය කිරීමට එය nginx ට උදව් කරන්නේ කෙසේද?
වෝල්ෆ්

රේල්ස් 5 යෙදුමට සේවය කිරීම සඳහා මම ප්‍රතිවිකුණුම් ප්‍රොක්සියක් ලෙස nginx භාවිතා කළෙමි. මෙය CORS සීමාව nginx වෙතින් නොව, පිටුපස රේල්ස් ඇප් වෙතින් පැමිණි විශේෂිත අවස්ථාවකි.
user9869932
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.