Nginx - Changing the server root make location root not working - nginx

I'm trying to setup phpmyadmin on my domain, and for some reason, I can't have the server root I want.
This doesn't work (404 on example.com/phpmyadmin without anything in the logs):
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
client_max_body_size 300m;
root /var/www/html;
index index.php index.html index.htm;
server_name example.com;
location / {
try_files $uri $uri/ =404;
}
location /phpmyadmin/ {
root /var/www/admin/;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
But if I change the server root to /usr/share/nginx/html it works...
Do you have any idea of what is happening?
Thank you for reading me.

Ok I fixed it, thanks to this post : nginx configuration with multiple location blocks
The reason was the ~ location for php files...
So here is the working code :
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
client_max_body_size 300m;
root /var/www/html;
index index.php index.html index.htm;
server_name example.com;
location / {
try_files $uri $uri/ =404;
}
location /phpmyadmin/ {
root /var/www/admin/;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

Related

Custom error page nginx ubuntu 18.04 not working. How to fix?

I added the code in /etc/nginx/sites-available/default for custom error page
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $fastcgi_script_name =404;
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
}
error_page 404 /custom_error_404.html;
location = /custom_error_404.html {
root /usr/share/nginx/html;
internal;
}
error_page 500 502 503 504 /custom_error_50x.html;
location = /custom_error_50x.html {
root /usr/share/nginx/html;
internal;
}
location /display505error {
fastcgi_pass unix:/path/does/not/exist;
}
}
and file 50x and 404 in here /usr/share/nginx/html
but the error page is not working
and it still shows up
404 Not Found
nginx/1.14.0 (Ubuntu)
how to fix this code?

nginx configuration for multiple sites one ip address and one server

nginx multiple websites on same port and one ip address
for example my ip 192.167.10.2
and we want to run site on same ip
nginx/html contain two project like below
1: project1
2: project2
we run default project1 on ip :192.167.10.2
and second project run like 192.167.10.2/project2
how do configuration in nginx config file
Here is a simple example.You could try
First config :
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/mywebsite1;
index index.php index.html index.htm;
server_name mywebsite1.de www.mywebsite1.de;
location / {
try_files $uri/ /index.php?$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/mywebsite1;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Second config:
server {
listen 80;
listen [::]:80 //#here Be carefull , only one default
root /var/www/mywebsite2;
index index.php index.html index.htm;
server_name mywebsite1.de www.mywebsite1.de;
location / {
try_files $uri/ /index.php?$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/mywebsite1;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Don't forget to restart server.

wp-admin redirects to 404 not found nginx 1.10.0

Can you please help me to edit my Nginx script.I have problem with access to https://myurl.wp-admin.php redirects to 404 not found nginx 1.10.0.
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
listen 443 ssl;
root /var/www/html;
index index.php index.html;
server_name _;
ssl_certificate /etc/nginx/ssl/csr/ssl_altr/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/csr/ssl_altr/nginx.key;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

nginx - DNS works for www. but not for http://

I have a server that is using ngnix and I have a configuration file:
server {
listen 80;
server_name http://mycoolwebsite.com/;
return 301 http://www.mycoolwebsite.com$request_uri;
}
server {
listen 80;
root /var/www/website/front/public;
index index.php index.html index.htm;
server_name www.mycoolwebsite.com;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Now when I enter http://www.mycoolwebsite.com the website shows just fine. Whenever I enter http://mycoolwebsite.com I get:
This site can’t be reached | server DNS address could not be found.
What could be the issue here?
Your server name in the first server block should be:
server_name mycoolwebsite.com;
Also, make sure that there is a DNS entry for mycoolwebsite.com that points to your servers IP. This may be separate to the entry for www.mycoolwebsite.com.

NGINX url rewrite on digitalocean

I have problem with url rewrite from example.com to www.example.com on nginx web server. I using new hosting digitalocean.com and still struggling with this...
I will be happy with every opinion.
There is my code:
server {
server_name example.com;
return 301 http://www.example.com$request_uri;
}
server {
server_name www.example.com;
root /usr/share/nginx/www;
index index.php index.html index.htm;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
location / {
try_files $uri $uri/ /index.html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I have error when I trying to restart nginx server:
Restarting nginx: nginx: [emerg] could not build the server_names_hash, you should
increase server_names_hash_bucket_size: 32
nginx: configuration file /etc/nginx/nginx.conf test failed
The server name is probably too long for the default values.
Modify the /etc/nginx/nginx.conf file by adding the following below the http:
increase server_names_hash_bucket_size: 64
Save this value and test using the -t
nginx -t
Also, watch for any saved default configurations in the sites-available folder which may cause issues.
Try the following:
server {
server_name example.com;
return 301 http://www.example.com$request_uri;
}
server {
server_name www.example.com;
root /usr/share/nginx/www;
index index.php index.html index.htm;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
location / {
try_files $uri $uri/ /index.html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Resources