NGINX 404 all pages - nginx

So I have two issues that I am trying to do. First I am trying to understand NGINX and use it for redirects and everything as well; however, if I try to remove the extensions from the php / html files it just downloads the file. If I use another thing to just try (without the file extension try) it just gives me 404s. I am not sure what the issue is nor what I am doing wrong.
First I'll show my remove php file extension issue:
server {
listen 443;
server_name XXX.XX.XX.XX;
root /var/www/html;
location / {
try_files $uri $uri.html $uri/ #extensionless-php;
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri =404;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
}
Here is my other code to fix the problem above (but just gives me 404s)
server {
listen 443;
server_name XXX.XX.XX.XX;
root /var/www/html;
index index.php index.html index.htm;
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_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;
}
}
XXX.XX.XX.XX is my the servers ip.
PS: I have read that I need the bottom section first to get the top section working.
Sources include:
How to remove both .php and .html extensions from url using NGINX?
https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-14-04
Ty for your help.
UPDATES:
Kuldeep KD - 770 /var/www/html (not solution but thanks)
Jignesh Joisar - Trying port 80 (failed but per usual thanks)

Check for necessary permissions on /var/www/html
you can change the permissions using
chmod -R 770 /var/www/html/
also check for the owner of directory you may have to change that as well, depends on user your nginx is using.
chown -R user:group /var/www/html

try to this one in 80 port
server {
listen 80 default_server;
root /var/www/html/;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
server_name localhost;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php$is_args$args;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
try_files $uri /index.php =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;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}

Related

Whats the correct way to write nginx configuration for wordpress?

I have a problem with configuring my wordpress nginx settings.
This is my nginx configuraton
server {
listen 80;
server_name wordpress.project;
access_log /var/log/nginx/wordpress.project/access.log;
error_log /var/log/nginx/wordpress.project/error.log;
root /var/www/wordpress.project;
location / {
index app_dev.php;
if (-f $request_filename) {
break;
}
rewrite ^(.*)$ /app_dev.php last;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
Hosts file is set as 127.0.0.1 wordpress.project
Permissions of folders are set
sudo chmod -R a+rw /var/www/wordpress.project
sudo chown -R www-data:www-data /var/www/wordpress.project
And symbolic link is set
sudo ln -s /etc/nginx/sites-available/wordpess.project /etc/nginx/sites-enabled/wordpess.project
And there is still an error when i try accessing wordpress.project saying
File not found
Project has classic wordpress structure of files
Is this correct configuration for wordpress in nginx and if its not what is the best way?
Try this
server
{
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/wordpress.project;
index index.php index.html index.htm;
server_name your_domain.com;
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$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/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Please restart Nginx after changing the configuration (sudo service nginx restart)

Laravel5 and PhpMyadmin on Digital Ocean LEMP stack

I tried to install phpMyadmin on my LEMP server using following tutorial
https://www.digitalocean.com/community/tutorials/how-to-install-phpmyadmin-on-a-lemp-server
but when I try to access myip/phpmyadmin I get page cannot be found
before this I installed laravel5 so anything i am typing after ip/anystring its being accessed by my laravel route page ,
So can you tell me how I can access the phpMyadmin
my ip is http://xxxxxxxx//phpmyadmin .
and my default page for nginx looks like this
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/laravel/public;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name xxxxxxx;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
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;
include fastcgi_params;
}
}
Please hlp me out
Thanks
I am also stuck in the same kind of situation. Paste this code before location ~ .php$ function and it's work perfectly fine.
Source : Laravel routes overwriting phpmyadmin path with nginx
location /phpmyadmin {
root /usr/share/nginx/html;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/nginx/html;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/nginx/html;
}
}

redirect certain domain to different folders failing in nginx

I have three subdomains like m.xyz.com, test.xyz.com, prod.xyz.com, main.xyz.com. If I redirect my sub domain then it automatically locates the default folder path of the web site. I want to redirect all sub domains to different folder location. I'm using nginx server in centos, even after creating four separate .conf file it is redirecting to the default location.
.conf file code is as follows:
server {
listen 80;
server_name m.xyz.com www.m.xyz.com;
root /usr/share/nginx/html/folder1/location1/;
index index.php index.html index.htm;
location / {
#try_files $uri $uri/ =404;
try_files $uri $uri.html $uri/ #extensionless-php;
index index.html index.htm index.php;
}
# redirect server error pages to the static page /50x.html
#
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
location ~ \.php$ {
try_files $uri =404;
#fastcgi_pass unix:/var/run/xyz/xyz-php-fpm.sock;
fastcgi_pass 192.168.1.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# rewrites
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
charset utf-8;
#access_log /var/log/nginx/log/host.access.log main;
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

nginx browser caching directive brings 404 not found

One of the sites I want to implement browser caching for images
has this particular server {} block on virtual.conf
server {
listen 80;
server_name www.example.net example.net;
location / {
root /var/www/example.net/public_html;
index index.html index.htm index.php;
}
error_page 404 /404.html;
location = /404.html {
root /var/www/example.net/public_html;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/example.net/public_html;
}
location ~* \.(jpg|jpeg|png|gif)$ {
expires 180d;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root /var/www/example.net/public_html;
error_log /var/www/example.net/public_html/error.log error;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Normally this directive
location ~* \.(jpg|jpeg|png|gif)$ {
expires 180d;
}
Should take care of caching images on the user end. But instead when I access
an image on my website I get 404 not found. I am not sure why this I caused.
Yes. I reloaded/restarted nginx after adding the directive.
The image resides in a subfolder like:
example.net/images/dir1/user_photos/photo.jpg
or
example.net/images/dir1/user_photos/photo.jpg
Any help would be appreciated.
Thank you.
Root parameter should be outside the location directive.
It is not the best practice to have root inside location
Change
location / {
root /var/www/html;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
To:
root /var/www/html;
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}

Nginx and Jekyll: serving in a subdomain

I'm currently stuck in configuring Jekyll and nginx to work together on a VPS.
Essentially, I set up everything and a git hook from my local machine to the VPS.
Here is my nginx config file:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www;
index index.php index.html index.htm;
server_name server_ip;
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;
}
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;
}
}
server {
listen 80;
server_name server_ip/blog;
location / {
root /var/www/blog;
index index.html index.htm;
}
}
Ideally, what I'd like is to serve what is in /var/www/blog at blog.domain.example, or blog.server_ip at the moment.
However, when jekyll build runs, the URLs are all wrong. I can see index.html at server_ip/blog, but the /blog/ bit in the URL is not replicated in the links on the Jekyll page.
For example, a post should live at server_ip/blog/2015/04/07/title, but the URL I'm given is server_ip/2015/04/07/title. Same goes for the CSS files, and images as well.
Thanks very much for your help.
You have to set baseurl: "/blog"
And link to posts like this <a href="{{site.baseurl}}{{post.url}}>....
And generaly use {{site.baseurl}} to built any url.

Resources