Search code examples
nginxsubdirectory

install postfixadmin in a subfolder of a domain in nginx


I need to install postfixadmin as a subfolder of a domain hosted in nginx.

In other words, I'm trying to access postfixadmin using http://example.com/postfixadmin

Physically, the contents of the sites are stored this way:

  • example.com site in /var/www/example
  • Postfixadmin files in /var/www/postfixadmin

I've tried adding this within the server section of example.com:

location ~ /posfixadmin/ {
    root /var/www;
    index index.php;
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
  }

The above works partially, php scripts are executed correctly, but css and image files found under /var/www/postfixadmin/css and /var/www/postfixadmin/images are not loaded.

I've checked at the generated html code and the links to css and image files in postfixadmin are called using relative paths, like this:

href="css/default.css"

I think nginx tries to get the css files from http://example.com/css instead of http://example.com/postfixadmin/css, that's why it's failing, I've tried something like this:

 location /postfixadmin/css/ {
    root /var/www/postfixadmin;
  }

but the above doesn't work.

Any idea about how to fix this? Thanks in advance!


Solution

  • I know it's an old topic, but still: do not use "root" in a "location" block. Source: https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/

    This works for me at the moment with the current PostfixAdmin 3.2 (which moved all the public facing stuff into the "public" subdirectory). Mind that I have defined the fastcgi_pass elsewhere, so this bit is not directly applicable.

    location /postfixadmin {
        alias /usr/local/www/postfixadmin/public;
    
        location ~ ^(.+\.php)(.*)$ {
           fastcgi_pass   php;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME $request_filename;
           include        fastcgi_params;
    
           fastcgi_split_path_info ^(.+\.php)(.*)$;
           fastcgi_param PATH_INFO $fastcgi_path_info;
           fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    
           fastcgi_read_timeout 180;
           fastcgi_buffers 4 256k;
           fastcgi_buffer_size 128k;
        }
    
        location ~ \.php {
           include        /usr/local/etc/nginx/fastcgi_params;
           fastcgi_param  SCRIPT_FILENAME $request_filename;
           fastcgi_pass   php;
           fastcgi_index  index.php;
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }
    }