Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I looked around at other examples and tried to replicate a very basic nginx server layout. However, it's not working at all (other than example.com, which correctly redirects to index.html).

Here's my nginx config file:

server {
    listen 80;
    root /var/www/example.com/public_html;

    server_name example.com;

    location / {
        root /var/www/example.com/public_html/templates;
        try_files $uri $uri/ /index.html;
    location /draw/ {
        root /var/www/example.com/public_html/templates/projects/draw;
        try_files $uri $uri/ /drawsomething.html;
    location ~ \.(gif|jpg|png)$ {
        root /var/www/example.com/public_html/templates/pic;

Basically, I store everything in /templates/. I want to make it so when someone goes to example.com/draw it displays drawsomething.html, which is in /var/www/example.com/public_html/templates/projects/draw. I followed the example on the website and it doesn't seem to be working. The redirect for images seems to be working, though. Also, what if I want to include script files? Could I just use:

location ~ \.js$ {
    root /var/www/example.com/public_html/templates/script;
share|improve this question

1 Answer 1

You should remove the trailing slash :

location /draw {
        root /var/www/example.com/public_html/templates/projects/draw;
        try_files $uri $uri/ /drawsomething.html;

eventually you can add an index file :

location /draw {
        root /var/www/example.com/public_html/templates/projects/draw;
        index drawsomething.html;
        try_files $uri $uri/ @backend;
share|improve this answer
I currently have root /var/www/example.com/public_html/templates/projects/draw; and index drawsomething.html; inside of the location /draw {...} block and it isn't working. Do I absolutely need to use try_files? –  royhowie Feb 19 '14 at 15:50
What is not working ? Here if there is images, the last location block will match before the /draw location block. Maybe it's what is not working !? –  DrGkill Feb 19 '14 at 15:56
It's returning 500: internal server error when I attempt to go to example.com/draw. I did sudo service nginx restart after I edited the config file. –  royhowie Feb 19 '14 at 18:02
Here's a link to my current config file setup. –  royhowie Feb 19 '14 at 18:04
I did put "@backend" in the try_files as an example since it seems logical you have one. Firstly remove it, then add "error_log /path/to/error.log" and see what error nginx report. –  DrGkill Feb 19 '14 at 19:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.