NGINX and PHP-FPM is downloading index.php instead of processing it - nginx

I recently installed NGINX and PHP-FPM on a Centos6 server. I'm able to view other php pages on my site, but for some reason my index.php file gets downloaded rather than processed like a normal php page.
Here is the nginx config:
# The default server
#
server {
listen 80 default_server;
server_name example.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /var/www/html/;
index index.php index.html index.htm;
}
error_page 404 /404.html;
location = /index.php {
root /var/www/html;
}
# redirect server error pages to the static page /50x.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$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
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 fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}

Try to remove this block:
location = /index.php {
root /var/www/html;
}

Related

Multiple conf files on Nginx not working properly

I'm trying to set up a docker container using Nginx default image and add a simple health route to it as proposed here.
My config is as follows, and I am placing it under /etc/nginx/conf.d/bacon.conf.
# bacon.conf
server {
listen 80;
location /health {
access_log off;
add_header 'Content-Type' 'application/json';
return 200 '{"status":"Healthy"}';
}
}
When I try to access this /health route I end up getting a 404.
I've noticed there is already a default conf file on etc/nginx/conf.d/ called default.conf.
# default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.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$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$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;
#}
}
If I remove this default file from my container or if I merge both of them on a single file, my /health route starts working just fine.
I also checked nginx.conf under /etc/nginx which has this line:
#nginx.conf
...
include /etc/nginx/conf.d/*.conf;
...
From what I understand this should make NGinx mix all files ending in .conf into a single configuration file, shouldn't it?
For the sake of simplicity I would like to keep both files, how can I achieve this?

Redirecting all requests to a static page with nginx

I'm trying to redirect all the requests hitting my page to an image. Inside my default.conf I have this:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
try_files $uri $uri/ /usr/share/nginx/html/10343632.jpeg;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.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$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$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;
#}
}
the jpeg file is the image and it's located in /usr/share/nginx/html and I have removed all the other files from that directory. Every time I hit the page it gives a :
rewrite or internal redirection cycle while internally redirecting to "/usr/share/nginx/html/10343632.jpeg
I've also tried :
try_files $uri /usr/share/nginx/html/10343632.jpeg;
and
try_files $uri /10343632.jpeg;
and got the same error. The point of this is to serve an image no matter what the user requested.
The location section must account for root such that :
location / {
root /usr/share/nginx/html/;
try_files $uri /10343632.jpeg;
}
and it'll work!

Nginx - root folder

When I am accesing page at http://myIP I am seeing files in folder
var/www/html
instead of
var/www/html/src/public
I have set up following setting in /etc/nginx/sites-available/default:
root /var/www/html/src/public;
index index.html index.htm;
and also restarted nginx.
What could be wrong? Thank you in advance.
You need to define your document root in:
/etc/nginx/nginx.conf
Or define it in your virtual host config file in:
/etc/nginx/conf.d/default.conf
Here is an example default.conf file setup:
server {
listen 80;
listen 443 ssl;
ssl on;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
server_name adamsawesomewebsite.tk;
root /var/www/vhosts/adamsawesomewebsite.tk/httpdocs;
index index.php index.html;
#charset koi8-r;
#access_log /var/log/nginx/access.log main;
# redirect server error pages to the static page /40x.html
#
#error_page 404 /404.html;
#location = /40x.html {
#}
location / {
try_files $uri $uri/ /index.php?q=$request_uri;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

Nginx downloads PHP

I installed Nginx on my server (Ubuntu) and after following a Digital Ocean tutorial, it still downloads any PHP rather than executing it. Here is my default.conf file:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name {domain_omitted};
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
location /RequestDenied {
proxy_pass http://127.0.0.1:8080;
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# root html;
# index index.html index.htm;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
Note: Yes, I did change my php.ini to reflect cgi.fix_pathinfo = 0;. I also cleared my cache, restarted nginx and php5-fpm and still nothing.
In /etc/nginx/sites-available/default I commented out the following line:
#fastcgi_pass 127.0.0.1:9000;

How to make phpmyadmin work with NGINX

I have tried many things but I cant seem to make it work.
I am running CentOS6 64-bit. Latest NGINX version installed.
I cannot seem to go to http://domain.com/phpmyadmin aftet switching from Apache.
Please help, thanks.
Here is my Default.conf:
# The default server
#
server {
listen 80 default_server;
server_name _;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /var/www/html;
index login.php index.php;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.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$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /var/www/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /var/www/html$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;
#}
}
EDIT:
FIXED
Moved the PHPMYADMIN folder from /usr/share to my web directory.
And now you broke the package. For prosperity:
location /phpmyadmin {
root /usr/share;
}

Resources