I have followed instruction for setting up django with nginx from the django wiki (https://code.djangoproject.com/wiki/DjangoAndNginx) and have nginx setup as follows (a few name changes to fit my setup).

2 Answers
2

Short answer: see the last location in the long version. Technically, the setup described in that page is wrong. See the end of the answer for reasons.

Long answer: you porbably have /etc/nginx... path in your logs because requests for static files don't match your static location(the regex location takes precedense) an you have not specified root for the server block itself.

When your request matches the regex location, nginx will search files under /etc/nginx. To fix this you might want to add root directive directly inside server block as described above, or under each non-proxied location (the same goes to fastCGI as well).

Also spotted: if you specify root directive under location nginx will search for files under $document_root/$location, e.g. in /srv/static/static which is 99% not you might want. You may use alias directive but nginx documentation prefers to redefine root directive if this is possible:

location /static {
root /srv;
}

The alias directive works just like you expected, so you might want to write this instead:

location /static {
alias /srv/static;
}

As to the regex location, you might want to put it inside /static location. Also since there will be only static files, you can get rid of ther regex location, and final location will look like this:

location /static {
root /srv;
access_log off;
expires 30d;
}

Why the described setup is wrong and how to fix this

According to the commands the user provided, he likes to have most of the components as fresh as possible running on a Debian-like Linux. But instead of correctly installing everything by hand or just using aptitude he starts mixing and installing software from packages as well. This will work for some development box but this is a no-go for any production. So here are some points:

Please install software in one way, preferrably from packages. If you need bleeding edge, build packages for them (that's not so hard ;) ) or use some environment to separate it from system like virtualenv.

You don't need to manually add nginx user and group. Web-servers run from www-data:www-data in Debian. Since your setup does not intend to run any code within web-server context, adding more users is useless.

The article suggests to redefine the whole /etc/nginx/nginx.conf file which is totally wrong, especially on Debian-like systems. Instead of doing this you might want to create sample_project file in /etc/nginx/sites-available and link it to /etc/nginx/sites-enabled to make nginx attach your configuration`:

The sample_project file in this case should contain only server block itself, nothing more.

Most of the directives (except log_format and maybe some others) should have context of your virtualhost and thus should be placed under server block in your sample_project file. You will not affect the work of other services running on this web-server. You may place log_format directive in your file but outside of any block and before the server block itself.

Instead of writing all that fastcgi_pass directives one should write include fastcgi_params and only redefine those that not correct to current setup.

To have your log files automatically rotating by logrotate you should name them to match *access.log or *error.log wildcard.