Search code examples
nginxuwsgi

Does nginx line ordering within a location matters?


Specifically I have the following location:

location / {
    root ...;
    try_files $uri $uri/ /index.html =404;
    ....
    add_header ....
    server_tokens off;
    # Does the order of the next 2 lines matter?
    include uwsgi_params;
    uwsgi_pass django;
  }

I'm wondering if the order of the lines in it matters. And specifically about the 2 last lines.


Solution

  • I could not find any documentation that answered the question "does the order of directives in a location block matter?". However, through studying the examples in the Nginx uwsgi module document, as listed below:

    location / {
        include    uwsgi_params;
        uwsgi_pass localhost:9000;
    }
    
    location / {
        uwsgi_pass        backend;
        uwsgi_cache       cache_zone;
        uwsgi_cache_key   $uri;
        uwsgi_cache_purge $purge_method;
    }
    
    location /fetch/ {
        internal;
    
        uwsgi_pass         backend:9000;
        ...
    
        uwsgi_store        on;
        uwsgi_store_access user:rw group:rw all:r;
        uwsgi_temp_path    /data/temp;
    
        alias              /data/www/;
    }
    

    I guess that the order of the uwsgi_pass directive and other uwsgi_param directives inside the uwsgi_params file is not important. As you see, the uwsgi_pass directive can be at the beginning, the middle, or the end of a block.

    Furthermore, in the article that explains how to use NGINX as an application gateway with uWSGI and Django, the uwsgi_pass directive can also lie between uwsgi_param directives, as follows:

    location /main {
        include /etc/nginx/uwsgi_params;
        uwsgi_pass django;
        uwsgi_param Host $host;
        uwsgi_param X-Real-IP $remote_addr;
        uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
        uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;
    }
    

    Based on my experience when setting up PHP websites with FastCGI, the order of the directives doesn't matter. However, if you declare the same directive more than once inside a block, the last declared directive will take effect.