NGINX仅为`api`子域设置CORS头

时间:2017-06-15 22:25:56

标签: nginx http-headers cors

我有一个NGINX设置代理我的应用服务器请求,如下所示:

daemon off;
#Heroku dynos have at least 4 cores.
worker_processes <%= ENV['NGINX_WORKERS'] || 4 %>;

events {
  use epoll;
  accept_mutex on;
  worker_connections 1024;
}

http {
  # Instead of using Rack::Deflater and having the application serer Gzip HTML and JSON requests from
  # the client, have the webserver compress them.

  gzip on;
  gzip_comp_level 3;
  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;

  underscores_in_headers on;

  server_tokens off;

  log_format l2met 'measure#nginx.service=$request_time request_id=$http_x_request_id';
  access_log logs/nginx/access.log l2met;
  error_log logs/nginx/error.log;

  include mime.types;
  default_type application/octet-stream;
  sendfile on;

  #Must read the body in 5 seconds.
  client_body_timeout <%= ENV['NGINX_CLIENT_BODY_TIMEOUT'] || 5 %>;

  upstream app_server {
      server unix:/tmp/nginx.socket fail_timeout=0;
  }

  server {
    listen <%= ENV["PORT"] %>;
    keepalive_timeout 5;
    root /app/public;
    client_max_body_size <%= ENV['NGINX_CLIENT_MAX_BODY_SIZE'] || 1 %>M;
    server_name _

    location ~ ^/(assets)/ {
      # Have Nginx prefer to serve the *.gz file, since its already compressed and ready to go
      gzip_static on;

      # Per RFC2616 - 1 year maximum expiry
      expires 1y;
      add_header Cache-Control public;

      # Some browsers still send conditional-GET requests if there's a
      # Last-Modified header or an ETag header even if they haven't
      # reached the expiry date sent in the Expires header.
      add_header Last-Modified "";
      add_header ETag "";

      # When serving fonts, we need to make sure we set the `Access-Control-Allow-Origin` header to '*'.
      location ~* \.(eot|svg|ttf|woff)$ {
        add_header 'Access-Control-Allow-Origin' '*' always;
      }

      break;
    }

    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://app_server;
    }
  }
}

但是,如果用户向api.mydomain.com发出请求,我希望NGINX为这些请求添加CORS标头,但只有这样。

因此,如果用户向app.mydomain.com发出请求,我不想要CORS标头,但如果他们向api.mydomain.com请求,我希望他们添加。{有没有人有关于如何实现这一目标的任何提示?

1 个答案:

答案 0 :(得分:3)

您可以为server添加特定api.mydomain.com块:

server {
  server_name api.mydomain.com;
  location / {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET,POST';
    add_header 'Access-Control-Allow-Headers' 'Content-Type,Authorization';
  }
}

或者我猜你可以使用if ($host = "api.mydomain.com")块:

if ($host = "api.mydomain.com") {
  add_header 'Access-Control-Allow-Origin' '*';
  add_header 'Access-Control-Allow-Methods' 'GET,POST';
  add_header 'Access-Control-Allow-Headers' 'Content-Type,Authorization';
}