nginx add_header not working

后端 未结 8 805
无人共我
无人共我 2020-12-23 16:00

I am having an intriguing problem where whenever I use add_header in my virtual host configuration on an ubuntu server running nginx with PHP and php-fpm it sim

相关标签:
8条回答
  • 2020-12-23 16:39

    It turns out that trying to update nginx to the newest version was causing this. I had tried previously to reinstall which seemed to reinstall it correctly but actually Ubuntu wasn't properly removing nginx. So all I had to do is reinstall Ubuntu server and install everything anew using just the standard ubuntu repositories.

    0 讨论(0)
  • 2020-12-23 16:40

    There were two issues for me.

    One is that nginx only processes the last add_header it spots down a tree. So if you have an add_header in the server context, then another in the location nested context, it will only process the add_header directive inside the location context. Only the deepest context.

    From the NGINX docs on add_header:

    There could be several add_header directives. These directives are inherited from the previous level if and only if there are no add_header directives defined on the current level.

    Second issue was that the location / {} block I had in place was actually sending nginx to the other location ~* (\.php)$ block (because it would repath all requests through index.php, and that actually makes nginx process this php block). So, my add_header directives inside the first location directive were useless, and it started working after I put all the directives I needed inside the php location directive.

    Finally, here's my working configuration to allow CORS in the context of an MVC framework called Laravel (you could change this easily to fit any PHP framework that has index.php as a single entry point for all requests).

    server {
        root /path/to/app/public;
        index index.php;
    
        server_name test.dev;
    
        # redirection to index.php
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    
            # With php5-fpm:
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    
            # cors configuration
            # whitelist of allowed domains, via a regular expression
            # if ($http_origin ~* (http://localhost(:[0-9]+)?)) {
            if ($http_origin ~* .*) { # yeah, for local development. tailor your regex as needed
                 set $cors "true";
            }
    
            # apparently, the following three if statements create a flag for "compound conditions"
            if ($request_method = OPTIONS) {
                set $cors "${cors}options";
            }
    
            if ($request_method = GET) {
                set $cors "${cors}get";
            }
    
            if ($request_method = POST) {
                set $cors "${cors}post";
            }
    
            # now process the flag
            if ($cors = 'trueget') {
                add_header 'Access-Control-Allow-Origin' "$http_origin";
                add_header 'Access-Control-Allow-Credentials' 'true';
            }
    
            if ($cors = 'truepost') {
                add_header 'Access-Control-Allow-Origin' "$http_origin";
                add_header 'Access-Control-Allow-Credentials' 'true';
            }
    
            if ($cors = 'trueoptions') {
                add_header 'Access-Control-Allow-Origin' "$http_origin";
                add_header 'Access-Control-Allow-Credentials' 'true';
    
                add_header 'Access-Control-Max-Age' 1728000; # cache preflight value for 20 days
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since';
    
                add_header 'Content-Length' 0;
                add_header 'Content-Type' 'text/plain charset=UTF-8';
                return 204;
            }
        }
    
        error_log /var/log/nginx/test.dev.error.log;
        access_log /var/log/nginx/test.dev.access.log;
    }
    

    The gist for the above is at: https://gist.github.com/adityamenon/6753574

    0 讨论(0)
  • 2020-12-23 16:43

    Evidently the add_header inheritance quirk/gotcha applies to the upstream layer as well.

    I had a script pre-authorizing requests meant for another service, and was therefore returning all of the headers from the other service.

    Once I started adding an 'Access-Control-Allow-Origin' entry along with these relayed headers, the browser would actually get the entry and allow the request.

    0 讨论(0)
  • 2020-12-23 16:44

    I had the issue of not getting the response header due to the response code not within the allowed range, unless you specify the "always" keyword after the header value.

    From the official docs:

    Adds the specified field to a response header provided that the response code equals 200, 201, 204, 206, 301, 302, 303, 304, 307, or 308. The value can contain variables.

    0 讨论(0)
  • 2020-12-23 16:48

    What does your nginx error log say?

    Do you know which add_header lines are breaking the configuration? If not, comment them all out then enable them 1 by 1, reloading nginx to see which one(s) is/are the problem. I would begin by commenting out the block:

    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
    add_header PS 1
    

    The problem could be that you're setting headers not supported by the core httpHeaders module. Installing the NginxHttpHeadersMoreModule may be helpful.

    Also, try replacing the two add_header lines int the location ~* \... with the following:

    add_header Pragma '';
    add_header Cache-Control 'public, max-age=31536000'
    

    Is there a reason you have the gzip configuration here and not in your global nginx.conf?

    0 讨论(0)
  • 2020-12-23 16:57

    When I test the above add_header settings with:

    # nginx -t && service nginx reload
    

    I get

    nginx: [emerg] directive "add_header" is not terminated by ";" in
    /etc/nginx/enabled-sites/example.com.conf:21
    
    nginx: configuration file /etc/nginx/nginx.conf test failed
    

    So the complain is reagarding this line:

    add_header PS 1
    

    missing the semi-colon (;)

    To test the headers I like to use

    # curl -I http://example.com
    

    According to the ngx_http_headers_module manual

    syntax: add_header name value;
    default:     —
    context:    http, server, location, if in location
    

    I further tried

    add_header X-test-A 1;
    add_header "X-test-B" "2";
    add_header 'X-test-C' '3';
    

    in the context of http, server and location, but it only showed up in the server context.

    0 讨论(0)
提交回复
热议问题