nginx server config with multiple locations does not work

37,676

Your goal is to completely separate your "regular" web files from your phpMyAdmin installation.

It should be stressed that each server configuration in Nginx can (and should) have only one webroot. That being said, these are your options:

  • Install phpMyAdmin in a directory under your webroot, which in your case is /var/www/phpmyadmin. It can be accessed through http://localhost/phpmyadmin

    This is the simplest configuration and I'm including it here for the sake of completeness (and people coming here from search engines).

  • Install phpMyAdmin in a directory outside your webroot and then create a symlink named phpmyadmin in your webroot pointing to that directory. In that case, you need to make sure that you have specified disable_symlinks off in your server configuration.

  • You can achieve separation on the same vhost by creating 2 server configurations listening on different ports, having different webroots and communicating through proxy_pass directive. A basic outline of such configuration is the following:

    server {
        listen   80;
        server_name localhost;
        root /var/www/htdocs;
        index index.php index.html index.htm;
    
        location /phpmyadmin {
            proxy_pass http://127.0.0.1:8080/;
        }
    
        # ...Add more location directives, php support, etc...
    }
    
    server {
        listen 8080;
        server_name localhost;
        root /var/www/phpmyadmin;
        index index.php index.html index.htm;
    
        # ...Specify additional location directives, php support, etc...
    }
    

    In this case, all requests to phpMyAdmin will be transparently passed to the server instance listening on port 8080 through the /phpmyadmin location in the server instance listening on port 80.

  • Finally you can achieve separation on different vhosts by creating 2 server configurations listening on the same port, but having different server_name directives and different root locations. For example, a basic outline like this:

    server {
        listen   80;
        server_name dev.local;
        root /var/www/htdocs;
        index index.php index.html index.htm;
    
        # ...Add more location directives, php support, etc...
    }
    
    server {
        listen   80;
        server_name phpmyadmin.local;
        root /var/www/phpmyadmin;
        index index.php index.html index.htm;
    
        # ...Specify additional location directives, php support, etc...
    }
    

Then, you would go ahead and add the following entries to your /etc/hosts:

127.0.0.1    dev.local
127.0.0.1    phpmyadmin.local

and then you can access your files through http://dev.local and your phpMyAdmin instance through http://phpmyadmin.local. Obviously, from your local workstation.

Share:
37,676

Related videos on Youtube

priyanka -
Author by

priyanka -

Updated on September 18, 2022

Comments

  • priyanka -
    priyanka - over 1 year

    I have been trying to get this to work for hours now!

    I would like to set up a simple web server. My web files shall be in /var/www. I also want to have phpmyadmin. I created a directory /var/phpmyadmin. Now I want to acces the normal web files in the standard way.
    For instance: The file /var/www/test.php should be accesible with http://localhost/test.php.
    The phpmyadmin part should be acces like this: http://localhost/phpmyadmin. With the config below I get a 404. Also with this URL: http://localhost/phpmyadmin/index.php

    For this I created this file in the sites-availble folder of nginx:

    server {
      listen 80; ## listen for ipv4; this line is default and implied
      listen [::]:80 default_server ipv6only=on; ## listen for ipv6
    
      root /var/www;
      index index.html index.htm index.php;
    
      try_files $uri $uri/ $uri/index.html $uri/index.htm $uri/index.php;
    
      # This didn't work
      location /phpmyadmin/ {
         alias /var/phpmyadmin;
      }
    
      # And this did neither. (Never used both at the same time!)
      location /phpmyadmin/ {
         root /var;
      }
    
      location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    
        # With php5-cgi alone:
        #fastcgi_pass 127.0.0.1:9000;
        # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
      }
    
      location ~ /\.ht {
        deny all;
      }
    }
    

    What am I doing wrong?

    Edit:

    Interesting to note is that this works (The root directory works (http://localhost)):

      root /var/www/htdocs;
    
      index index.php index.html index.htm;
    
      location /phpmyadmin/ {
        root /var/www/phpmyadmin;
      }
    

    And this doesn't:

      index index.php index.html index.htm;
    
      location / {
        root /var/www/htdocs;
      }
    
      location /phpmyadmin/ {
        root /var/www/phpmyadmin;
      }
    

    phpmyadmin still doesn't work!

  • priyanka -
    priyanka - almost 10 years
    Still doesn't work!
  • priyanka -
    priyanka - almost 10 years
    I update my question with more results from my attempts to get it to work!
  • priyanka -
    priyanka - almost 10 years
    I would still like to keep my phpmyadmin files out of my webfiles directory! This must be possible!
  • priyanka -
    priyanka - almost 10 years
    And example would be awesome! (You gotta earn your reputation ;) )
  • priyanka -
    priyanka - almost 10 years
    And still it's supposed to work. So why doesn't it?
  • dkaragasidis
    dkaragasidis almost 10 years
    @BrainStone I edited my answer once more in order to demonstrate how to achieve what you want using proxy_pass and different server directives.
  • priyanka -
    priyanka - almost 10 years
    Thank you once again. It somehow works but there are still problems: The root page of phpMyAdmin loads and all other pages (Like http://localhost/phpmyadmin/index.php) still get a 404. Also because of this the page stays blank. PHP is working though! But there must be an easy way to get this to work with a single server and just having the right settings.
  • dkaragasidis
    dkaragasidis almost 10 years
    @BrainStone Did you re-install phpMyAdmin after switching to the new configuration and webroot or were you just moving around your phpmyadmin directory since the time you first installed it? If not, do so.
  • priyanka -
    priyanka - almost 10 years
    I just reinstalled it. And it still would be nice to have a working nginx config similar to the one I tried to make.
  • dkaragasidis
    dkaragasidis almost 10 years
    @BrainStone I made a final update to my answer, listing all the options that I know of. If it still doesn't work, you are very welcome to read the fine manuals yourself, wait for other people to reply or experiment on your own. Regarding your "reputation" comment, I'm not doing this for imaginary points but because I tend to finish what I start. Good luck, I hope you solve your server issues soon.
  • priyanka -
    priyanka - almost 10 years
    The symlink option seems best for the moment once I managed to get my domain on the server I will go with the different server name option! Thank you anyways.