我的nginx版本:nginx/1.4.6
我在为多个子域启用 CORS 时遇到问题。我检查了https://gist.github.com/algal/5480916和http://rustyrazorblade.com/post/2013/2013-10-31-cors-with-wildcard-domains-and-nginx/但两种解决方案都没有不适合我。
它看起来像正则表达式
if ($http_origin ~* (.*\.mydomain.com)) {
set $cors "true";
}
Run Code Online (Sandbox Code Playgroud)
不匹配且 $cors 未设置为“true”,因此不会执行 add_header 'Access-Control-Allow-Origin' "$http_origin"。
我也试过正则表达式
$http_origin ~* (https?://.*.mydomain.com)
或者
$http_origin ~* https?://.*.mydomain.com
但在任何一种情况下,正则表达式都不匹配,$cors 永远不会设置为“true”。
我错过了什么?
我的 nginx 配置 - 花括号中的域名(正在被 Ansible 取代):
upstream varnish {
server localhost:80;
}
server {
listen 443 default;
server_name {{vhost}};
ssl on;
ssl_certificate /etc/ssl/certs/ssl.{{domain}}.crt;
ssl_certificate_key /etc/ssl/private/{{domain}}.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#ssl_prefer_server_ciphers on;
# workaround remote exploit. Fixed in 1.5.0, 1.4.1
#
# http://mailman.nginx.org/pipermail/nginx-announce/2013/000112.html
if ($http_transfer_encoding ~* chunked) {
return 444;
}
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
proxy_redirect off;
# CORS
set $cors "";
if ($http_origin ~* (.*\.{{domain}})) {
set $cors "true";
}
location / {
# Set the max size for file uploads (/admin, /webmail)
client_max_body_size 10G;
proxy_pass http://varnish;
if ($cors = "true") {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow_Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range, X-CSRF-Token';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
}
if ($request_method = OPTIONS) {
return 204;
}
}
location = /50x.html {
root html;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 8
if在 NGINX 中使用内部位置块时会发生一些意想不到的事情。不推荐。这是一个使用map. https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/和https://agentzh.blogspot.com/2011/03/how-nginx-location-if-works.html
此设置允许我向 my-domain.com 和 localhost(用于开发)上的任何子域和任何端口发出请求。
map $http_origin $allow_origin {
~^https?://(.*\.)?my-domain.com(:\d+)?$ $http_origin;
~^https?://(.*\.)?localhost(:\d+)?$ $http_origin;
default "";
}
server {
listen 80 default_server;
server_name _;
add_header 'Access-Control-Allow-Origin' $allow_origin;
# ...
}
Run Code Online (Sandbox Code Playgroud)
http://nginx.org/en/docs/http/ngx_http_map_module.html
小智 5
通过使用这种允许所有子域的巧妙解决方法,您可以绕过只有一个子域的限制:
server {
root /path/to/your/stuff;
index index.html index.htm;
set $cors "";
if ($http_origin ~* (.*\.yoursweetdomain.com)) {
set $cors "true";
}
server_name yoursweetdomain.com;
location / {
if ($cors = "true") {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'User-Agent,Keep-Alive,Content-Type';
}
if ($request_method = OPTIONS) {
return 204;
}
}
}
Run Code Online (Sandbox Code Playgroud)
信用:http : //rustyrazorblade.com/post/2013/2013-10-31-cors-with-wildcard-domains-and-nginx/
| 归档时间: |
|
| 查看次数: |
31926 次 |
| 最近记录: |