1

I have self-compiled nginx 1.9.14 with http2 support:

nginx version: nginx/1.9.14
built by gcc 4.9.2 (Debian 4.9.2-10) 
built with OpenSSL 1.0.1k 8 Jan 2015
TLS SNI support enabled

So I have a location = / block in my config, which should add a header only when the user accesses the root site (https://myserver.example.com/). However when I access my server in Firefox the header in the / location block (which is WhereIAm) is not added:

Content-Encoding: gzip
Content-Type: text/html
Date: Thu, 21 Apr 2016 07:12:11 GMT
Etag: W/"570bf892-363"
Last-Modified: Mon, 11 Apr 2016 19:18:42 GMT
Server: nginx
Strict-Transport-Security: max-age=15768000; includeSubDomains;  preload
X-Content-Type-Options: nosniff
X-Firefox-Spdy: h2
headerend: AtTheEnd
testing: IsTLS
x-something: ALsoAtTheEnd

This is my nginx config:

server {
        listen 80;
        listen [::]:80;

        server_name myserver.example.com;
        root /var/www/html;

        server_tokens off;

        add_header testing IsHTTP;
}

server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;

        server_name myserver.example.com;
        root /var/www/html;

        server_tokens off;

        index index.php index.html index.htm;

        # -- TEST BEGIN --

        location = /configtest {
                #echo "itworks";
                add_header itworks configtest;
        }

        add_header testing IsTLS;

        location = / {
                add_header WhereIAm Is:/;
        }
        location /testdir1 {
                add_header WhereIAm Is:/testdir1;
        }
        location = /testdir2 {
                add_header WhereIAm Is:/testdir2;
        }

        # -- TEST END --


        # -- PARSER --

        location ~ \.php$ {
                #location ~ \..*/.*\.php$ {return 404;}
                include snippets/fastcgi-php.conf;
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
        }

        # -- HTTPS --

        # certs sent to the client in SERVER HELLO are concatenated in ssl_certificate
        ssl_certificate /path/to/my/certificate;
        ssl_certificate_key /path/to/my/privatekey;
        ssl_session_timeout 1d;
        ssl_session_cache shared:SSL:50m;
        ssl_session_tickets off;

        # HSTS (ngx_http_headers_module is required) (15768000 seconds = 6 months)
        add_header Strict-Transport-Security "max-age=15768000; includeSubDomains;  preload";

        # OCSP Stapling ---
        # fetch OCSP records from URL in ssl_certificate and cache them
        ssl_stapling on;
        ssl_stapling_verify on;

        ## verify chain of trust of OCSP response using Root CA and Intermediate certs
        ssl_trusted_certificate /path/to/the/rootandintermediatecerts;

        # more headers
        add_header X-Something ALsoAtTheEnd;
        add_header HeaderEnd AtTheEnd;
        add_header X-Content-Type-Options nosniff;

        # deny access to Apache config files
        location ~ /\.ht {
                deny all;
        }
}

So in contrast when I access https://myserver.example.com/testdir1/ (and testdir1 is a directory, which exists on my server) it adds the correct header:

Content-Encoding: gzip
Content-Type: text/html
Date: Thu, 21 Apr 2016 07:24:22 GMT
Etag: W/"571538f2-363"
Last-Modified: Mon, 18 Apr 2016 19:43:46 GMT
Server: nginx
X-Firefox-Spdy: h2
whereiam: Is:/testdir1

What's more confusing is when I change the location block to this:

location = / {
    echo "ThisIsATest";
    add_header WhereIAm Is:/;
}

(I've compiled nginx with the echo-nginx-module)

Nginx does not only return the test string when accessing it, but it also adds the correct header:

Content-Type: application/octet-stream
Date: Thu, 21 Apr 2016 07:32:01 GMT
Server: nginx
X-Firefox-Spdy: h2
whereiam: Is:/

So what is going on here and how can I get nginx to only add a header when a user accesses /?

rugk
  • 506
  • 2
  • 6
  • 18
  • 1
    `index` directive makes internal redirect so nginx actually serves `/index.php` URI – Alexey Ten Apr 21 '16 at 09:20
  • @Alexey Thanks. Indeed I could solve my problem by changing the `/` to my index file path. This essentially solves my problem. But there is still a thing I don't understand: The previous configuration worked with the `echo` command. Usually the echo should also not be executed as `/`is not caught. So why did `echo` caused the location block to execute? – rugk Apr 21 '16 at 17:27
  • @rugk the `echo` directive comes not from nginx, but from some 3rd-party module. It can have lower quality of code and unpredictable behaviour. – VBart Aug 14 '16 at 09:03
  • @VBart Okay. But we should give them the change to improve the quality: https://github.com/openresty/echo-nginx-module/issues/56 – rugk Aug 14 '16 at 15:16
  • *chance of course – rugk Aug 14 '16 at 15:23
  • 1
    So the result of https://github.com/openresty/echo-nginx-module/issues/56#issuecomment-239890619 is: This behaviour is expected. – rugk Aug 15 '16 at 19:30

0 Answers0