ini 示例Nginx配置,用于为反向代理API添加跨源资源共享(CORS)支持

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ini 示例Nginx配置,用于为反向代理API添加跨源资源共享(CORS)支持相关的知识,希望对你有一定的参考价值。

#
# CORS header support
#
# One way to use this is by placing it into a file called "cors_support"
# under your Nginx configuration directory and placing the following
# statement inside your **location** block(s):
#
#   include cors_support;
#
# As of Nginx 1.7.5, add_header supports an "always" parameter which
# allows CORS to work if the backend returns 4xx or 5xx status code.
#
# For more information on CORS, please see: http://enable-cors.org/
# Forked from this Gist: https://gist.github.com/michiel/1064640
#

set $cors '';
if ($http_origin ~ '^https?://(localhost|www\.yourdomain\.com|www\.yourotherdomain\.com)') {
        set $cors 'true';
}

if ($cors = 'true') {
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
        # required to be able to read Authorization header in frontend
        #add_header 'Access-Control-Expose-Headers' 'Authorization' always;
}

if ($request_method = 'OPTIONS') {
        # 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;
}

ini nginx代理缓存配置文件示例

proxy_cache_path /tmp/nginx_cache keys_zone=cache_one:100m
                       loader_threshold=300 loader_files=200
                                            max_size=200m;
  server {
          listen       80;
          server_name  www.tidev.in tidev.in;
          client_max_body_size       500m;
          charset utf-8;
          location / {
              proxy_hide_header "cache-control";
              proxy_hide_header Expires;

              proxy_ignore_headers Set-Cookie;
              proxy_ignore_headers Cache-Control;
              proxy_ignore_headers Expires;
              proxy_ignore_headers X-Accel-Expires;

              proxy_cache cache_one;
              proxy_cache_valid any   60s;  # 任何内容,都缓存60秒钟

              proxy_pass          http://tidev_servers;
              proxy_redirect      default;
              proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_set_header    X-Real-IP $remote_addr;
              proxy_set_header    Host $http_host;
              proxy_next_upstream http_502 http_504 error timeout invalid_header;

              proxy_cache_key $host$uri$is_args$args;
          }
          location ~ ^/assets/ {
              root /opt/app/tidev.in/public;
              expires 1y;
              add_header Cache-Control public;
              add_header ETag "";
              break;
          }
  }

  upstream tidev_servers{
         server localhost:3600;
         server localhost:3601;
         server localhost:3602;
         server localhost:3603;
  }
  
  # 参考
  #http://siwei.me/blog/posts/nginx-nginx-content-caching
  #http://wanbei.github.io/2016/08/15/nginx_proxy_cache/

以上是关于ini 示例Nginx配置,用于为反向代理API添加跨源资源共享(CORS)支持的主要内容,如果未能解决你的问题,请参考以下文章

ini 示例Nginx配置,用于为反向代理API添加跨源资源共享(CORS)支持

ini 示例Nginx配置,用于为反向代理API添加跨源资源共享(CORS)支持

ini 示例Nginx配置,用于为反向代理API添加跨源资源共享(CORS)支持

宝塔面板Nginx反向代理解决跨域问题

nginx的反向代理功能具体示例

nginx代理api