I have a local development environment with the followings installed:
In nginx.conf I included the /etc/nginx/conf.d/*.conf to successfully separate config files from each other. I have yet only one config file which contains configurations to all of my localhost websites from "phpmyadmin" to under development wordpress sites and my goal is to reach them as directories properly. I'm sure that I have problems in my poorly made nginx configuration, probably I should define a rewrite rule for url parameters but I can't find a solution:
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/localhost-access_log main;
error_log /var/log/nginx/localhost-error_log info;
root /var/www/localhost/htdocs/;
index index.php index.html index.htm;
location ~ \.php$ {
# Test for non-existent scripts or throw a 404 error
# Without this line, nginx will blindly send any request ending in .php to php-fpm
try_files $uri =404;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:run/php-fpm.socket;
}
location /phpmyadmin {
alias /var/www/localhost/htdocs/phpmyadmin;
access_log /var/log/nginx/phpmyadmin-access_log main;
error_log /var/log/nginx/phpmyadmin-error_log info;
}
location /samplewordpress {
alias /var/www/localhost/htdocs/samplewordpress;
access_log /var/log/nginx/samplewordpress-access_log main;
error_log /var/log/nginx/samplewordpress-error_log info;
}
location /szpetra {
alias /var/www/localhost/htdocs/szpetra;
access_log /var/log/nginx/szpetra-access_log main;
error_log /var/log/nginx/szpetra-error_log info;
}
}
I can log in to the admin page and everything works fine, but after I try to load a page or a post, it returns me an "Access denied" message.
Any ideas to solve this problem while I can keep the website in directories?
Thank you for your answer @r3wt . However, I guess that the configuration file above is improper I wan't to keep it as it is (yet for development seems like okay for me, for deploying I'd rather use separated config files for every site).
So, the problem with the alias part and I should've define try_files properly as I want to catch links starts with localhost/szpetra so I added it and uncommented the alias part cause now I clearly understand what is the difference between root and alias whis is:
This will result in files being searched for in /foo/bar/bar as the full URI is appended.
location /bar {
root /foo/bar;
}
This will result in files being searched for in /foo/bar as only the URI part after /bar is appended.
location /bar {
alias /foo/bar;
}
Source: https://forum.nginx.org/read.php?2,129656,130107
Now the modified config file looks like this:
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/localhost-access_log main;
error_log /var/log/nginx/localhost-error_log info;
root /var/www/localhost/htdocs/;
index index.php index.html index.htm; # I used index definition here so I think I shouldn't define it under a 'location' field.
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:run/php-fpm.socket;
}
location /phpmyadmin {
alias /var/www/localhost/htdocs/phpmyadmin;
access_log /var/log/nginx/phpmyadmin-access_log main;
error_log /var/log/nginx/phpmyadmin-error_log info;
}
location /samplewordpress {
alias /var/www/localhost/htdocs/samplewordpress;
access_log /var/log/nginx/samplewordpress-access_log main;
error_log /var/log/nginx/samplewordpress-error_log info;
}
location /szpetra {
# alias /var/www/localhost/htdocs/szpetra; #uncommenting alias helped me solve the problem
access_log /var/log/nginx/szpetra-access_log main;
error_log /var/log/nginx/szpetra-error_log info;
# index index.php index.html index.htm; # this doesn't need cause I defined the indexes above in the server{} section but I'm not sure that about this
try_files $uri $uri/ /szpetra/index.php?$args; # Adding this line helped me solve the problem
}
}