0

I have the following files on my web server:

/var/www/html/--+
                |
                +-misc--+
                |       |
                |       +-misc1--+
                |       |        |
                |       |        +-index.html
                |       |
                |       +-misc2--+
                |       |        |
                |       |        +-index.php
                |       |
                |       +-misc3.php
                |
                +-wordpress--+
                             |
                             +-index.php

I have Nginx set up such that http://example.com/ goes to my Wordpress install. On my previous (Apache) setup I was able to easily create aliases to point to the items in misc but I'm unsure how to do this in Nginx.

    index index.php index.html;
    root /var/www/html/wordpress;

    location ~ [^/]\.php(/|$) {
        limit_except GET POST {}
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
        }

        fastcgi_buffer_size 16k;
        fastcgi_buffers 16 16k;

        fastcgi_param    SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param    PATH_INFO          $fastcgi_path_info;
        fastcgi_param    PATH_TRANSLATED    $document_root$fastcgi_path_info;
        fastcgi_param    SERVER_NAME        $host;
        fastcgi_param    HTTP_PROXY         "";

        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~* \.(?:css|js|jpg|jpeg|gif|png|mp4)$ {
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
    }

    location / {
        try_files $uri $uri/ /index.php;
        limit_except GET {}
    }

What I'd like is:

What I've tried:

# http://example.com/misc1 shows index.html but anything else in the folder is 404
location /misc1 {
    alias /var/www/html/misc/misc1;
}

# http://example.com/misc1 gives 403, http://example.com/misc1/index.html gives 404
location /misc1/ {
    alias /var/www/html/misc/misc1;
}

# shows Wordpress 404 page
location /misc1/* {
    alias /var/www/html/misc/misc1;
}

# gives 403 error
location ~ /misc1 {
    alias /var/www/html/misc/misc1;
}

Nothing I've tried has had any effect on PHP, it does not work.

miken32
  • 930
  • 1
  • 11
  • 32

2 Answers2

1

Not sure why you're using alias. Try this, it's not tested but it should get you at least closer to what you're trying to achieve. If it doesn't work comment with details on why it doesn't work, and show applicable logs and curls.

root /var/www/html/misc
try_files $uri $uri/; # NB This can be specified at the server or location level

location / {
  root /var/www/html/wordpress;
  try_files $uri $uri/ /index.php?$args;
}

location /misc1/ {
  root /var/www/html/misc;
}

location /misc2/ {
  root /var/www/html/misc;
}

Edit Based on the comment "As soon as I change the root directive at the server scope, I get 404 on my Wordpress site, as though it's ignoring the root directive on the location scope." you could try this. I use this technique when I have a custom PHP app in the root directory with Wordpress in a subdirectory.

root /var/www/html/;
location / {
  try_files $uri $uri/ /wordpress/index.php?$args;
}
Tim
  • 30,383
  • 6
  • 47
  • 77
  • I think you mean `root /var/www/html/misc;` – Michael Hampton Jan 19 '17 at 23:58
  • @MichaelHampton the question is inconsistent between the tree and the locations in the URL section. I've modified my answer a little (a few times), it should be better now. I think it'd require a bit of playing around and tweaking to get right, but it should be close. – Tim Jan 20 '17 at 00:02
  • I updated the question with correct file paths, thanks. As soon as I change the `root` directive at the server scope, I get 404 on my Wordpress site, as though it's ignoring the `root` directive on the location scope. – miken32 Jan 20 '17 at 00:31
  • @miken32 see my edited answer. I think you may just need to fiddle around with root contents and locations to get it right. I'd expect root in a location block to override root at the server level. – Tim Jan 20 '17 at 00:39
  • Using `root` instead of `alias` definitely is getting me closer. Debug logging tells me the `location ~ [^/]\.php(/|$)` block is getting used, and the `location /` is getting ignored, with the result that the `$document_root` variable doesn't get updated with the correct value, thus the 404 on Wordpress. Will take a look tomorrow, thanks for the help so far! – miken32 Jan 20 '17 at 01:00
0

So as it turns out, location blocks based on regular expressions will always override other location blocks. My configuration had two regex locations: the block meant to enable caching on static resources, and the block meant to enable PHP. Since one of these blocks matched all the content being served, every other location block was being ignored!

What I ended up doing is nesting location blocks, placing the static file and PHP blocks inside the location blocks. Now my config looks like this:

php.inc

location ~ [^/]\.php(/|$) {
    limit_except GET POST {}
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }

    fastcgi_buffer_size 16k;
    fastcgi_buffers 16 16k;

    fastcgi_param    SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param    PATH_INFO          $fastcgi_path_info;
    fastcgi_param    PATH_TRANSLATED    $document_root$fastcgi_path_info;
    fastcgi_param    SERVER_NAME        $host;
    fastcgi_param    HTTP_PROXY         "";

    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

static.inc

location ~* \.(?:css|js|jpg|jpeg|gif|png|mp4)$ {
    expires 1M;
    access_log off;
    add_header Cache-Control "public";
}

example.conf

index index.php index.html;
root /var/www/html/wordpress;

location / {
    try_files $uri $uri/ /index.php;
    limit_except GET {}
    include conf.d/php.inc;
    include conf.d/static.inc;
}

location /misc1 {
    root /var/www/html/misc/;
    include conf.d/static.inc;
}

location /misc2 {
    root /var/www/html/misc/;
    include conf.d/php.inc;
    include conf.d/static.inc;
}

location /misc3.php {
    root /var/www/html/misc/;
    include conf.d/php.inc;
}
miken32
  • 930
  • 1
  • 11
  • 32