nginx configure default error pages - nginx

First off, first time nginx user. So, I'm still learning the differences from Apache and nginx.
I have a stock install of nginx. (apt-get install nginx-full) I modified the default configuration found at '/etc/nginx/sites-enabled/default' for my setup. However, error pages just don't work. My docroot is /server/www, and my error pages are located within the directory /server/errors/. Below is my config.
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /server/www;
index index.html;
server_name website.com;
location / {
try_files $uri $uri/ /index.html;
}
error_page 403 404 405 /40x.html;
location /40x.html {
root /server/errors;
internal;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /server/errors;
internal;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
All pages that should generate a 403 or 404 page, just load the index.html file. I believe the something is probably happening with 500 errors, however, that's a bit harder to generate.
Also, if it's relevant, I moved the error pages above docroot, but they weren't working before that. I made that move as the 'internal' flag didn't appear to make the pages internal as the documentation claimed, as I could still access them directly.
Any advise/help you could give would be great!

The error issue was related to the location / having an end value of /index.html. Essentially, nginx was first trying the uri as a file, then as a directory, and then reverting to index.html. Instead I wanted it to return a 404. So I changed it to the below and now it works.
location / {
try_files $uri $uri/ =404;
}

Related

I add nginx config of domain but my domain is not going online

I want to add a domain manually with cli of centos 7 and created a config file in sites-available directory:
server {
listen 80;
server_name hustana.ir www.hustana.ir;
location / {
root /home/www/public_html;
index index.html index.htm;
try_files $uri $uri/ =404;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
In my server there is another domains that are online and works fine.
if its help I should tell that at start my server was worked by Plesk Obtisian control panel
but now I wanna add another domain to server just but it`s not working.
I changed my domain NS from domain control center.
additional description:
I guess it`s good to say that if I delete the config block of one of my older domains , domain still stay online but it displays just a blank white page.
I tried add to include this config file from another directory directly into nginx.conf:
server {
listen 80;
server_name hustana.ir;
root /home/www/public_html;
index index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
You need to enable your site as mentioned here.
Check the A-record of your domain. It must point to your server IP.

Upgraded NginX from 1.18.0 to 1.23.0 on Ubuntu 20.04 LTS - Wordpress page URLs all non-functional post-upgrade

Once I finished doing the above the associated wordpress site's permalinks stopped working.
The wordpress site itself and all .php and .html files render fine, it is only that any sub-page (except for the home page) is now invalid and clicking any wordpress page link generates 404 errors on the Wordpress site.
Curiously, the wordpress "/admin" page still works 100% correctly under both NginX 1.18.0 and NginX 1.23.0 - with the admin back-end ALSO using what looks like permalinks...
This is my /etc/nginx/conf.d/default.conf for the new instance, I used the same file as was working on my 1.18.0 install on my 1.23.0 Ubuntu 20.04 based install:
server {
listen 80;
root /var/www/wordpress;
server_name www.myserver.com
client_max_body_size 100M;
autoindex off;
location / {
root /var/www/wordpress;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args =404;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
location ~ /\. {
deny all;
}
}
Several suggested fixes have already been incorporated for fixing permalinks in Wordpress under Nginx, e. g. these are Google-ed suggestions:
try_files $uri $uri/ /index.php?$args =404;
iso just
try_files $uri $uri/ =404;
and adding
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
to the location ~ .php$ block.
Nothing works. Any sub-page URL I try to access on the site gives a 404.
Sub-page URLs are typical wordpress URLs, e.g.
www.myserver.com/home/top-content
www.myserver.com/home/get-in-thouch
etc. - these are all now broken and result in 404s if clicked when this wordpress site is deployed under NginX 1.23.0 in Ubuntu 20.04.
The above worked correctly with NginX 1.18.0 and Wordpress 6.0.1, NginX 1.23.0 cannot / does not provide Wordpress permalink functionality with the above /etc/nginx/conf.d/default.conf config.
What am I missing to get WordPress permalink functionality back under NginX 1.23.0 - clearly there is some differences in how that is configured in NginX 1.23.0 vs. NginX 1.18.0?
Thanks!
Ok, this was solved by adding
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
to my /etc/nginx/conf.d/default.conf config file, then restarting NginX and php8.1-fpm.
E. g. my /etc/nginx.conf.d/default.conf file under NginX 1.23.0. needs the above added to provide the same wordpress permalink functionality as I had with the same PHP and Wordpress instances under NginX 1.18.0.
My /etc/nginx.conf.d/default.conf in a NginX 1.23.0 wordpress permalink compatible state is
server {
listen 80;
root /var/www/wordpress;
server_name www.msbmanagement.co.za;
client_max_body_size 100M;
autoindex off;
location / {
root /var/www/wordpress;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args =404;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
#THIS WAS REQUIRED TO BE ADDED FOR NGINX 1.23.0 BEGIN
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
#THIS WAS REQUIRED TO BE ADDED FOR NGINX 1.23.0 END
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
location ~ /\. {
deny all;
}
}
Hope this helps someone. Apparently quite a few differences are extant as regards wordpress permalink URL rewriting in NginX 1.18.0 vs NginX 1.23.0.

Why would Nginx serve files correctly from the default directory but not new directory?

Installation
OS: CentOS 7.4
Server: Nginx 1.12.2
Question: My nginx installation serves files without problem from the default directory, /usr/share/nginx/html. Unfortunately I get a 403 error when I use a new folder that I created, /www/html. What should I look for?
My permissions are identical for both folders although the owner is different.
Original default folder
drwxr-xr-x. 13 root root 155 Jan 8 09:25 usr
New default folder
drwxr-xr-x. 3 first first 18 Jan 15 10:45 www
I am using the stripped down server.conf file below and it works correctly.
events {}
http {
server {
listen 80;
server_name mydomain.com;
root /usr/share/nginx/html;
}
}
However when I change server.conf to use this directory /www/html I get a 403 error.
Thanks for any help in advance! I'm a noob and not even sure where to look beyond file permissions.
I faced the same problem.
Solution. You must create test.example.com.conf inside the conf.d folder
sudo nano /etc/nginx/conf.d/test.example.com.conf
And the file :
server {
listen 80;
#listen *:80;
server_name test.example.com;
# note that these lines are originally from the "location /" block
root /var/www/test.example.com/;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
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-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
restart server systemctl restart nginx.service

why does nginx sets root folder to wrong location?

I want to have two servers on my machine, one private, accessible only locally on 127.0.0.1 and one which is visible on LAN (it's root folder is subfolder of the private server). So I have made two configuration files in sites-available and linked them to sites-enabled folder. File accessible:
server {
listen 80;
root /usr/share/nginx/html/accessible/;
index index.php index.html index.htm;
server_name accessible;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html/accessible/;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
and file localhost:
server {
listen 127.0.0.1:80;
listen [::1]:80;
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
After that I have updated /etc/hosts so that http://accessible/ is forwarded to 127.0.0.1: 127.0.0.1 accessible is the line.
Now when I try to connect to http://localhost/, everything is ok, I get /usr/share/nginx/html/index.html as expected. But when I try to connect to http://accessible/ The same file /usr/share/nginx/html/index.html is displayed. How is that possible? The root folders are apparently set.
The problem is that localhost server operates on the same IP as I have configured for accessible to be redirected to which is apparently not possible (or at least I don't know how).
I have redirected accessible to 127.0.0.2 (changed line in /etc/hosts to 127.0.0.2 accessible) which is another address served by local machine and after small change in nginx config file accessible that allowed all LAN IP addresses (allow 192.168.1.0/32;) everything works just fine (for me on local machine and for computers on network).

NGINX path separator/parsing

In Apache (without mod_rewrite) I could utilize URI's such as this:
/module/erp/service.php/application/workorder/list?start=0&limit=25
What do I have to configure or change to support this with NGINX?
server {
listen 80;
root /usr/share/nginx/www/web/public;
index index.php index.html;
server_name apps.mydomain.com;
location / {
try_files $uri $uri/ /index.html;
}
#error_page 404 /404.html;
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/www;
#}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Can someone show me what I might need to add in order to achieve this requirement?
p.s-I need to populate the $_SERVER['PATH_INFO'] just as Apache does as well...
EDIT |
I believe I found my answer here:
https://askubuntu.com/questions/164627/nginx-php-fpm-access-denied-error
I then encountered "access denied" which sounds like the resolve is here:
https://askubuntu.com/questions/164627/nginx-php-fpm-access-denied-error
Any ideas???
OK the links I provided above walked me through the process of resolving my issue with odd URI configuration.
The issue was the cgi.fix_pathinfo=0
Most articles I read suggest this is a gapping security hole -- in the case where one might have uploads. I do not have uploads enabled.
http://wiki.nginx.org/PHPFcgiExample
Security trade-off? Perhaps...in my case it wasn't as important as the URI handling as Apache did.

Resources