1

Good evening,

~ What I would like to do ~

I need a default location block matching anything, location / {} I guess, this block has its own root directory. it has to serve any static file and interpret any php files inside its document root or subfolders. (I set this up and it seams to works fine)

And here is where I need some help :(

Inside this same server block, I would like to host a php symphony application served when hitting mydomain.foo/panel/ area. So I added a ^~ /panel/ location (uri starts with /panel/)

My problem is that the try_files does not behave as I expect ( internal redirect to the last nested location in case uri do not match the nested location) . See code comments below.

~ My configuration as is … ~

upstream phpfcgi {
    server unix:/var/run/php5-fpm.sock;
}

server {
    listen 80;
    server_name domain.tld;

    ## Default locations
    location / {
        root /home/me/sites/default/www;
        index index.php;
        location ~ \.php$ {
            include fastcgi.conf;
            fastcgi_pass phpfcgi;
        }
        location ~ /(.+) {
            try_files $uri $1 =404;
        }
    }

    ## panel area
    location ^~ /panel/ {
        # Public files are located in sf/web sub-directory
        alias /home/me/sites/sf/web/;

        # Since uri starts with '/panel/', I expect NGINX to execute try_files as a last resort.
        # if nested location do not match, and uri is not an existing file,
        # shouldn't nginx do an internal redirect with the 2nd try_files argument
        # and finally hit the nested location (triggering fastcgi_pass) ?
        # But try_files is ignored and nginx falls back to "/" location above instead.
        try_files $uri /panel/app_dev.php$is_args$args;

        # FastCGIpass to upstream
        location ~ (app_dev|config)\.php(/|$) {
            try_files $fastcgi_script_name =404;
            set $path_info $fastcgi_path_info;
            fastcgi_param PATH_INFO $path_info;
            include fastcgi.conf;
            fastcgi_pass phpfcgi;
        }
    }

    # Custom errors
    error_page 403 404 =404 @null;
    error_page 324 500 502 503 504 =500 @null;
    location @null {
        root /home/me/sites/_;
        rewrite ^.+$ /$status.html break;
        try_files $uri 444;
    }
}

If my configuration is too clumsy, then how can I fulfill my requirements ? (default location with document root, and a special area serving my php application, on the same server) ?

NGINX documentation is getting better, but there still are things I do not understand in the request processing flow.

Any solution / suggestion / improvement ?

Thank you :)

0

You're using nested location blocks. I haven't used them because I don't really understand them, but they seem to be adding complexity. Try using location blocks that aren't nested. Your config looks quite different to any other I've seen, but my nginx experience is limited.

I have an example of my config in this question.

Nginx HHVM Wordpress issue with PHP Execution in one intermediate subdirectory

There's some best practice here.

That question also includes an example of how to debug these things, by adding headers and looking at them. Makes things a lot easier to understand and debug. If that's all you get from my answer I think it will be valuable.

| improve this answer | |
  • Thank you @tim, I finally split up /panel into two distinct location blocks and set up my configuration based on this post. – Stphane Jan 17 '16 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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