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.

My site content (which is currently kept in /public_html loosely) is under a Mercurial repository, so I would like it to have its own directory. Since my wiki is under /public_html/wiki, I am trying to come up with routing logic that will route all requests not going to mysite.com/wiki to /public_html/content.

Currently I have:

server {                                                                                                                                                                                                                                                 
    listen 80;                                                                                                                                                                                                                                           
    server_name www.mysite.com mysite.com;                                                                                                                                                                                             
    access_log /www/mysite.com/logs/access.log;                                                                                                                                                                                             
    error_log /www/mysite.com/logs/error.log;                                                                                                                                                                                               
    root /www/mysite.com/public_html;                                                                                                                                                                                                       

    location / {                                                                                                                                                                                                                                         
        index index.html index.htm index.php;                                                                                                                                                                                                            
    }                                                                                                                                                                                                                                                    

    location ~/\.hg                                                                                                                                                                                                                                      
    {                                                                                                                                                                                                                                                    
        deny all;                                                                                                                                                                                                                                        
    }                                                                                                                                                                                                                                                    

    location ~ \.php$ {                                                                                                                                                                                                                                  
        try_files  $uri  /404.html;                                                                                                                                                                                                                      
        include /etc/nginx/fastcgi_params;                                                                                                                                                                                                               
        fastcgi_pass 127.0.0.1:9000;                                                                                                                                                                                                                     
        fastcgi_index index.php;                                                                                                                                                                                                                         
        fastcgi_param SCRIPT_FILENAME /www/mysite.com/public_html$fastcgi_script_name;                                                                                                                                                      
    }                                                                                                                                                                                                                                                    
}

I tried try_files $uri /content$uri but that just made it loop back in on itself with rewriting. I also couldn't catch /wiki prefix with a location block because I would have had to stop the pattern matching at that point, preventing the .php from being matched.

share|improve this question

2 Answers 2

Just determine $document_root for every location and use this variable in location for php/fastcgi:

server {
    listen 80;
    server_name www.mysite.com mysite.com;
    access_log /www/mysite.com/logs/access.log;
    error_log /www/mysite.com/logs/error.log;

    location / {
        root /www/mysite.com/public_html;
        index index.html index.htm index.php;
    }

    location /wiki {
        root /www/mysite.com/wiki;
        index index.php;
    }

    location ~/\.hg {
            deny all;
    }

    location ~ \.php$ {
        try_files  $uri  /404.html;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
share|improve this answer
    
This kept blowing up on the try_files $uri /404.html; line; $uri failed and it went to 404. I eventually found something that worked that I will post. –  mars Jun 14 '12 at 23:42
up vote 0 down vote accepted

This is what ended up working for me:

server {
    listen 80;
    server_name www.mysite.com mysite.com;
    access_log /www/mysite.com/logs/access.log;
    error_log /www/mysite.com/logs/error.log;
    root /www/mysite.com;

    location / {
        try_files /public_html$uri =404;
        index index.html index.htm index.php;
    }

    location /wiki {
        index index.php;
    }

    location ~/\.hg
    {
        deny all;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
share|improve this answer

Your Answer

 
discard

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.