Nginx – How to setup a different root for an Nginx location

nginxphp-fpm

I'm setting up my local web dev server using NGINX and PHP-FPM on OS X.
I've installed both services and set up a virtual for localhost domain. So far everything works:

  • nginx is running correctly
  • it's able to read files from the custom root configured for localhost server_name
  • php files are processed correctly

The next thing that I'm trying to set up is an alias for the /phpmyadmin path on the localhost domain. I would like for the url http://localhost/phpmyadmin to load its contents from /usr/local/share/phpmyadmin rather than from the default configured root.

I've added this location block in my localhost server configuration:

location /phpmyadmin {
    alias    /usr/local/share/phpmyadmin;
    include   /usr/local/etc/nginx/conf.d/php-fpm;
}

but the response on http://localhost/phpmyadmin requests is 404.

Here are the configs that I used:

/usr/local/etc/nginx/nginx.conf

worker_processes  2;

error_log  /usr/local/etc/nginx/logs/error.log debug;

events {
    worker_connections  1024;
}

http {
    include             mime.types;
    default_type        application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /usr/local/etc/nginx/logs/access.log  main;

    sendfile            on;

    keepalive_timeout   600;

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 2;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_min_length 256;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype
image/svg+xml image/x-icon;

    index index.html index.php;

    include /usr/local/etc/nginx/sites-enabled/*;
}

/usr/local/etc/nginx/sites-available/default

server {
    listen       80;
    server_name  localhost;
    root       /Users/sebi/www/localhost;

    access_log  /usr/local/etc/nginx/logs/default.access.log  main;
    error_log  /usr/local/etc/nginx/logs/default.error.log debug;

    location / {
        include   /usr/local/etc/nginx/conf.d/php-fpm;
    }

    location /phpmyadmin {
        alias    /usr/local/share/phpmyadmin;
        include   /usr/local/etc/nginx/conf.d/php-fpm;
    }

    error_page  404     /404.html;
    error_page  403     /403.html;
}

/usr/local/etc/nginx/conf.d/php-fpm

location ~ \.php$ {
    try_files      $uri = 404;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

* UPDATE *
I figured out that my location block for /phpmyadmin was misconfigured. I need to point it to /usr/local/share like this:

location /phpmyadmin {
    alias    /usr/local/share;
    include   /usr/local/etc/nginx/conf.d/php-fpm;
}

but this still doesn't work and I found out that it's due to how the path is configured. It's a relative symlink. If I create a new symlink to the absolute path, it will work.

Examples:

/usr/local/share/phpmyadmin -> ../Cellar/phpmyadmin/4.7.4/share/phpmyadmin doesn't work resulting in a directory index of "/usr/local/share/" is forbidden
error

/usr/local/share/pma -> /usr/local/Cellar/phpmyadmin/4.7.4/share/phpmyadmin works.

Any ideas on how to configure nginx to allow reading from relative symlinks?

Best Answer

Add index index.php to your PHPMyAdmin block. This tells nginx what filename to use for requests asking for a directory.