Nginx 403 forbidden using Location directive on PHP file - php

Here's my server block:
server {
listen 80;
server_name localhost;
root /www/html;
index index.php;
location = / {
autoindex on;
index index.php;
try_files $uri $uri/ =404;
}
location ~*\.(css|js|gif|jpe?g|png)$ {
root /www/html/shared;
rewrite ^/releases(.*)$ $1 last;
expires 5m;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Right now, doing this will just spit out a "403 Forbidden" error when I attempt to access "localhost/". However, the page works perfectly fine if I access it directly-- "localhost/index.php". I can't for the life of me figure out why. The error.log doesn't give me any helpful info nor an error code either--only that the page is "forbidden".
Here's the thrown error:
2017/03/29 14:37:35 [error] 11980#8840: *94 directory index of "C:/www/html/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
This is a windows-based nginx server.

the answer was kind of disappointing to discover, but i'm gonna answer it so if anyone encounters this goofy issue they can fix it.
php was lookng for files with the prefix "/www/html", NOT "/www/html/", as a result when it wanted my index it was trying to load "/www/htmlindex.php" instead of "/www/html/index.php".
you can fix this by doing either
fastcgi_index /index.php;
or adding a / to the end of your root.

Related

PHP with gitlab bundled nginx not working

I've setup gitlab using the omnibus package on CentOS 7. I'd like to use the gitlab server to host additional websites. I've enabled custom nginx conf by adding the below code to /etc/gitlab/gitlab.rb
nginx['custom_nginx_config'] = "include /etc/nginx/conf.d/*.conf;"
I've also created conf files in /etc/nginc/conf.d. Static HTML files are working but when i try to run php scripts, I'm getting a File not found - 404 error.
Following is the nginx conf for php :
server{
listen 80;
server_name example.com;
root /var/www/vhosts/example;
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 /opt/gitlab/embedded/html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /opt/gitlab/embedded/conf/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
The following is the error log:
FastCGI sent in stderr: "Primary script unknown" while reading response from upstream, client x.x.x.x, server: example.com, request: "GET / HTTP/1.1", upsteam: "fastcgi://127.0.0.1:9000", host: "example.com"
Maybe your problem comes from your "location ~ .php$" config.
You already fix the first problem with gitlab omnibus by included the right fatscgi_params instead of the default. Now it seems to comes from the location config.
Try the following code for your configuration :
location ~ \.php$ {
#in your case maybe : /opt/gitlab/embedded/html
root [YOUR APP DIRECTORY];
try_files $uri $uri/ /index.php?$args;
index index.html index.htm index.php;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_intercept_errors on;
include /opt/gitlab/embedded/conf/fastcgi_params;
}
This fix works for me on a Debian 8 server.
Hope it could help.

Php email form on nginx

I have a static html page that has a php form to send an email, the server is nginx and site loads as expected however when I attempt to send an email via the form I get a 404 error,
in the console; POST http://example.com/form-handler.php 404 (Not Found)
and the nginx error.log;
2014/04/07 14:31:19 [error] 10199#0: *48 open()
"/home/deployer/example/current/404.html" failed (2: No such file or
directory), client: 86.131.120.178, server: example.com, request:
"POST /form-handler.php HTTP/1.1", host: "example.com", referrer:
"http://example.com/"
this is my nginx conf for the site;
server {
listen 80;
root /home/deployer/example/current;
index index.php index.html index.htm;
server_name example.com www.example.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 ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
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 don't know why it cant find the form...

Why does nginx return a 403 error on the index.php file?

Calling "http://test.local/" in the browser returns a "403 Forbidden" error.
Calling "http://test.local/index.php" works well.
If I create a "index.html" file, calling "http://test.local/" shows me the "index.html" file correctly.
So just the "index.php" file doesn't work.
This is my vhost configuration:
server {
listen 80;
server_name test.local;
root /var/www/public;
index index.html index.htm index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
The error.log file contains this error:
2013/12/28 14:00:57 [error] 2854#0: *50 directory index of "/var/www/public/" is forbidden, client: 10.0.2.2, server: test.local, request: "GET / HTTP/1.1", host: "test.local"
Why's that? I've googled a lot now and checked permissions (which are all set to 777) and tried lots of other stuff too, but I don't get it working.
I'm new to nginx so probably I'm just missing something.
Thanks a lot!
Your configuration seems fine, but I think it can be simplified, try this
server {
listen 80;
server_name test.local;
root /var/www/public;
index index.php;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}

Nginx Wordpress Configuration, PHP file in theme directory is not passed to FastCGI

I am new to NGINX configs so bear with me. Below is my configuration which works fine for the whole site:
server {
listen ...;
server_name funkyoslo.no;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/share/nginx/funkyoslo.webbr.org/html;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/funkyoslo.webbr.org/html/;
}
# 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;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/funkyoslo.webbr.org/html/$fastcgi_script_name;
include fastcgi_params;
}
}
However, I am trying to load the file /wp-content/themes/funkyoslo/load-songs.php and it's giving me a 500 internal server error. I've checked the error logs and evidently the file isn't passed to FastCGI at all.
I tried adding the following block to no avail:
location ~ .*/wp-content/themes/funkyoslo/.*\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/funkyoslo.webbr.org/html/wp-content/themes/funkyoslo/$fastcgi_script_name;
include fastcgi_params;
}
Any help is greatly appreciated!
Look I don't know what's wrong with your config but try this and hopefully it should work, this is the minimal config.
Notes:
Removed the index to the server block level, check link to know why
Removed root to server block level, check link to know why
Removed all extra config from the php block level, by trials I realised that I only need those two I've written.
Added http:// to fastcgi_pass, I don't know if it's really required but I got used to writting it this way.
Edit: Ok apparently I don't need the http://, I just checked.
-
server {
listen 80;
server_name funkyoslo.no;
root /usr/share/nginx/funkyoslo.webbr.org/html;
error_page 500 502 503 504 /50x.html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$request_uri;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass http://127.0.0.1:9000;
}
}

nginx location alias: static files ignored?

im trying to get the zabbix-frontend to work with nginx.
this here is my nginx conf:
server {
listen 80;
server_name localhost;
root /var/www/test/htdocs;
index index.php index.html index.htm;
location /zabbix {
alias /usr/share/zabbix;
index index.php;
error_page 403 404 502 503 504 /zabbix/index.php;
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
expires epoch;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location ~ \.(jpg|jpeg|gif|png|ico)$ {
access_log off;
expires 33d;
}
}
location / {
try_files $uri $uri/ /index.php;
include /etc/nginx/proxy_params;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_read_timeout 120;
include /etc/nginx/fastcgi_params;
}
include /etc/nginx/security;
include /etc/nginx/main_rules;
}
the php scripts in /zabbix are working! but files like /usr/share/zabbix/css.css are NOT being served (404). in the error log is this:
2013/07/19 20:23:33 [error] 13583#0: *1 open() "/var/www/test/htdocs/zabbix/css.css" failed (2: No such file or directory), client: xxx, server: localhost, request: "GET /zabbix/css.css HTTP/1.1"
so as we can see, nginx is looking for the file in the main root directory /var/www/test/htdocs/ instead of in the alias directory /usr/share/zabbix/.
why is that so and how can i fix that?
I would try to separate them locations. /zabbix with alias only and ~ ^/zabbix/*.php$ with fastcgi. Both outside the / location with root.

Categories