nginx:add_header 指令不起作用

nginx:add_header 指令不起作用

我的问题:
nginx add_header 指令似乎不起作用

我已尝试过:
在我的 nginx 配置中我有这个:

location ~* \.(ttf|woff|eot|otf|woff2|svg|svgz)$ {
        access_log /var/log/nginx/fonts.access.log;
        add_header Access-Control-Allow-Origin *; expires 1M;
}

当我请求字体资源时,如下所示:

curl -i -s -D -  -XGET http://my.server.com/assets/my_font-f748f9b5f469637888371ef2a5a81765.eot -o /dev/null
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: max-age=2592000
Content-Type: application/octet-stream
Date: Tue, 08 Sep 2015 16:42:55 GMT
ETag: "55eee9fb-d980"
Expires: Thu, 08 Oct 2015 16:42:55 GMT
Last-Modified: Tue, 08 Sep 2015 14:00:27 GMT
Server: nginx/1.4.6 (Ubuntu)
Content-Length: 55680
Connection: keep-alive

请注意,我没有返回 Access-Control-Allow-Origin * 标头。为了确认我的 nginx 是否从该位置块返回,我添加了位置块日志记录。我确实在 font.access.log 中看到了对字体的请求。

$ tail -1 fonts.access.log
172.31.27.203 - - [08/Sep/2015:16:42:55 +0000] "GET /assets/my_font-f748f9b5f469637888371ef2a5a81765.eot HTTP/1.1" 200 55680 "-" "curl/7.30.0"

其他信息:
Nginx 版本和编译标志:

$ nginx -V
nginx version: nginx/1.4.6 (Ubuntu)
built by gcc 4.8.2 (Ubuntu 4.8.2-19ubuntu1)
TLS SNI support enabled
configure arguments: 
--with-cc-opt='-g -O2 -fstack-protector 
--param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' 
--with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' 
--prefix=/usr/share/nginx 
--conf-path=/etc/nginx/nginx.conf 
--http-log-path=/var/log/nginx/access.log 
--error-log-path=/var/log/nginx/error.log 
--lock-path=/var/lock/nginx.lock 
--pid-path=/run/nginx.pid 
--http-client-body-temp-path=/var/lib/nginx/body 
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi 
--http-proxy-temp-path=/var/lib/nginx/proxy 
--http-scgi-temp-path=/var/lib/nginx/scgi 
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi 
--with-debug 
--with-pcre-jit 
--with-ipv6 
--with-http_ssl_module 
--with-http_stub_status_module 
--with-http_realip_module 
--with-http_addition_module 
--with-http_dav_module 
--with-http_geoip_module 
--with-http_gzip_static_module 
--with-http_image_filter_module 
--with-http_spdy_module 
--with-http_sub_module 
--with-http_xslt_module 
--with-mail 
--with-mail_ssl_module

我的配置很好:

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

我的网站配置:

real_ip_header X-Forwarded-For;
log_format mysite_log_fmt '[$time_local][$status][$request] from="$remote_addr" host="$host" ua="$http_user_agent"';

upstream unicorn_mysite {
 server unix:/srv/www/mysite/shared/sockets/unicorn.sock fail_timeout=0;
}

server {
  listen 80;
  server_name
  <ip>
  <other ip>
  <domain name>
  <elb domain name>

  access_log /var/log/nginx/mysites.access.log mysite_log_fmt;

  keepalive_timeout 5;

  root /srv/www/mysite/current/public/;

  location ~* \.(ttf|woff|eot|otf|woff2|svg|svgz)$ {
        access_log /var/log/nginx/fonts.access.log;
        add_header Access-Control-Allow-Origin "*";
  }

  location / {
    try_files $uri/index.html $uri/index.htm @unicorn;
    access_log /var/log/nginx/slash.access.log;
  }

  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    access_log /var/log/nginx/unicorn.access.log;

    proxy_read_timeout 60;
    proxy_send_timeout 60;

    # If you don't find the filename in the static files
    # Then request it from the unicorn server
    if (!-f $request_filename) {
      proxy_pass http://unicorn_mysite;
      break;
    }
  }

  location /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    deny all;
  }

  error_page 500 502 503 504 /500.html;
  location = /500.html {
    root /srv/www/mysite/current/public/;
  }
}

答案1

我使用默认的 nginx 配置和您的位置设置在我的机器上工作。我猜这可能是由于继承行为?您可以发布一个迷你 nginx conf 来讨论吗?

答案2

解决了!

这毕竟不是配置问题。nginx 配置没问题。

我的建筑物网络应该受到指责,不知何故,由于未知的原因,他们正在剥离一些标题。

当从该网络外部访问时,一切正常,字体的响应标头已到位。

相关内容