• 请不要在回答技术问题时复制粘贴 AI 生成的内容
Keppel
V2EX  ›  程序员

求助,关于 Nginx 需要访问静态资源鉴权的一个配置问题

  •  
  •   Keppel · Mar 19, 2021 · 1732 views
    This topic created in 1883 days ago, the information mentioned may be changed or developed.

    server { listen 14011; server_name localhost;

         #charset koi8-r;
    
         #access_log  logs/host.access.log  main;
    
        location / {
            root   /;
            index  index.html index.htm;
        }
    
         location /upload {
         
         if ($request_method = 'OPTIONS') {
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
            add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
            return 204;
         }
    
            auth_request /auth;
    
            root   /;
        }
    
    
        location /auth {
        internal;
        proxy_set_header Host $host;
        proxy_pass_request_body off;
        proxy_set_header Content-Length "";
        proxy_pass http://127.0.0.1:14010/auth;
        }
    
     }
     
     
     
    

    ====================================================================== 以上是我的配置,因为需要访问静态资源要鉴权,所以需要携带请求头发请求,但是发送 OPTIONS 请求的时候跨域了,加上了网上说的配置还是不行,求助。

    7 replies    2021-03-23 09:18:50 +08:00
    Keppel
        1
    Keppel  
    OP
       Mar 19, 2021
    QAQ
    ch2
        2
    ch2  
       Mar 19, 2021
    把 if ($request_method = 'OPTIONS') 去掉
    add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    这条就够了,外面不用再套个 if
    Keppel
        3
    Keppel  
    OP
       Mar 19, 2021
    @ch2 试过,不起作用,试过各种方式,都不行,所以来求助了。
    NjcyNzMzNDQ3
        4
    NjcyNzMzNDQ3  
       Mar 19, 2021
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';

    # !!!!重要 普通 Ajax 请求直接发起请求不用添这个设置。
    # Vuejs 会先发起 option 请求,而 add_header 支持的 http 状态码有限,必须添加 http_code 返回 204
    if ($request_method = 'OPTIONS') {
    return 204;
    }
    NjcyNzMzNDQ3
        5
    NjcyNzMzNDQ3  
       Mar 19, 2021
    试试我这个,我之前就这么配置的
    Keppel
        6
    Keppel  
    OP
       Mar 19, 2021
    @NjcyNzMzNDQ3 我能说这个我也试过吗 =。=
    Keppel
        7
    Keppel  
    OP
       Mar 23, 2021
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Methods *;
    add_header Access-Control-Allow-Headers *;
    add_header Access-Control-Allow-Credentials: true;
    if ($request_method = 'OPTIONS') {
    return 204;
    }


    最后改成了这样解决的,和#4 差不多,但是全通配了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2892 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 13:38 · PVG 21:38 · LAX 06:38 · JFK 09:38
    ♥ Do have faith in what you're doing.