0

in Nginx config file, did set rewrite location like below but do not work and every time return 404 not found, what is the problem here?

server {   
listen 80;
server_name mydomain.com www.mydomain.com;
root /var/www/html/mydomain.com;
.
.
.
location /postfixadmin {
    root /var/www/html/postfixadmin/;
    index index.php index.html index.htm;
    location ~ ^/postfixadmin/(.+\.php)$ {
       try_files $uri $uri/ =404;
       root /var/www/html/postfixadmin/;
       fastcgi_pass 127.0.0.1:9000;
       fastcgi_index index.php;
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       include /etc/nginx/fastcgi_params;
       fastcgi_buffer_size 128k;
       fastcgi_buffers 256 4k;
       fastcgi_busy_buffers_size 256k;
       fastcgi_temp_file_write_size 256k;
       fastcgi_intercept_errors on;

    }
    location ~* ^/postfixadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
       root /var/www/html/postfixadmin/;
    }
}
location /PostfixAdmin {
   rewrite ^/* /postfixadmin last;
}
.
.
}
sIiiS
  • 111
  • 1
  • 2
  • 6

2 Answers2

2

There are several problems in your configuration.

First, you are using root directive inside locations. This is most likely the reason for 404 errors.

You should change the first location block to start like this:

location /postfixadmin {
    alias /var/www/html/postfixadmin/;

Then, you should remove the root directive from the first inner location block.

First inner location start should be like this:

location ~ ^/postfixadmin/.+\.php$ {
    try_files $uri =404;

You don't need the second inner location block at all, since the alias is inherited from the parent location block.

Finally, you should change the last location to look like this:

location /PostFixAdmin {
    rewrite ^ /postfixadmin last;
}
Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
0

problem solved by changing root location

correct config is :

server {   
listen 80;
server_name mydomain.com www.mydomain.com;
root /var/www/html/mydomain.com;
.
.
.
location /postfixadmin {
    root /var/www/html/;
    index index.php index.html index.htm;
    location ~ ^/postfixadmin/(.+\.php)$ {
       try_files $uri $uri/ =404;
       root /var/www/html/;
       fastcgi_pass 127.0.0.1:9000;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include /etc/nginx/fastcgi_params;
       fastcgi_buffer_size 128k;
       fastcgi_buffers 256 4k;
       fastcgi_busy_buffers_size 256k;
       fastcgi_temp_file_write_size 256k;
       fastcgi_intercept_errors on;

    }
    location ~* ^/postfixadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
       root /var/www/html/;
    }
}
location /PostfixAdmin {
   rewrite ^/* /postfixadmin last;
}
.
.
}
sIiiS
  • 111
  • 1
  • 2
  • 6