美文网首页
Nginx 跨域配置示例

Nginx 跨域配置示例

作者: 思考蛙 | 来源:发表于2021-04-19 23:19 被阅读0次
# DO NOT RESPOND TO REQUESTS OTHER THAN yourdomain.com
server {
    listen       80  default;
    server_name  _;
    return       444;
}

# FILE UPLOADS
server {
        listen   80;
        server_name  yourdomain.com www.yourdomain.com;

        access_log  /var/log/nginx/s3uploadproxy.access.log;
        error_log /var/log/nginx/s3uploadproxy.error.log;

        proxy_read_timeout 10;
        proxy_connect_timeout 10;
        # allow big files...
        client_max_body_size 30M;
        # and slow uploads
        proxy_send_timeout 1200;

        # Deny illegal Host headers
        if ($host !~* ^(yourdomain.com|www.yourdomain.com)$ ) {
                return 444;
        }

        # dissalow methods
        if ($request_method !~ ^(OPTIONS|POST)$ ) {
                # empty response
                return 444;
        }

        location / {

                # CORS PRE-FLIGHT REQUESTS
                if ($request_method = 'OPTIONS') {
                        more_set_headers 'Access-Control-Allow-Origin: *';
                        more_set_headers 'Access-Control-Allow-Methods: POST, OPTIONS';
                        more_set_headers 'Access-Control-Max-Age: 1728000';
                        more_set_headers 'Content-Type: text/plain; charset=UTF-8';
                        #more_set_headers 'Access-Control-Allow-Headers: ';
                        return 200;
                }

                # FILE UPLOADS
                if ($request_method = 'POST') {
                        more_set_headers 'Access-Control-Allow-Origin: *';
                        proxy_pass http://your-s3-bucket;
                }
        }

        # 204 (No Content) for favicon.ico
        location = /favicon.ico {
                #empty_gif;
                return 204;
        }
}

相关文章

网友评论

      本文标题:Nginx 跨域配置示例

      本文链接:https://www.haomeiwen.com/subject/dyfglltx.html