nginx - codeigniter 404 error for all urls - php

I have uploaded my files(Codeigniter Framework) in abcd directory ( url : example.com/abcd ) . All the links inside give 404 error . For removing index.php, I have placed this nginx.conf file in my root directory ( on server : /.../abcd/)
Here is the code for the nginx.conf file :
server {
server_name example.com/abcd;
root /..../abcd;
index index.html index.php index.htm;
# set expiration of assets to MAX for caching
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
# Check if a file exists, or route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~* \.php$ {
fastcgi_pass example.com/abcd;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
In apache, it was easy using rewrite, i am now stuck with nginx. Please help, I am using nginx for the first time

Try below code.. taken from
https://serverfault.com/questions/695521/codeigniter-in-subdirectory-on-nginx-404
location /abcde/ {
alias /var/www/abcde/;
try_files $uri $uri/ /abcde/index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass backend;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}

Related

Nginx with worpress as sub-directory returning 404 in posts

After adding various configurations into the Wordpress subdirectory, I can access without any issues the WP homepage, but I'm still not able to access the posts, returning 404.
www.test.com/blog - WP homepage, works perfectly fine
www.test.com/blog/test-post-for-blog - WP Post, 404
Here is the config:
server {
listen 80;
listen [::]:80;
server_name www.test.com;
location ^~ /media {
alias /var/local/test/static/media;
}
location ^~ /icons {
alias /var/local/test/static/icons;
}
location /blog {
alias /var/www/html;
index index.html index.htm index.php;
try_files $uri $uri/ /blog/index.php?$args /blog/index.php?q=$uri&$args;
# try_files $uri $uri/ /blog/index.php?q=$uri$args;
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_index index.php;
fastcgi_pass blog-test:9000;
fastcgi_split_path_info ^(/blog)(/.*)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
}
And the log:
2022/02/09 14:47:25 [error] 32#32: *45 open() "/etc/nginx/html/index.php" failed (2: No such file or directory), client: 10.10.10.10, server: www.test.com, request: "GET /blog/test-post-for-blog/ HTTP/1.1", host: "www.test.com", referrer: "https://www.test.com/blog/"
10.10.10.10 - admin [09/Feb/2022:14:47:25 +0000] "GET /blog/test-post-for-blog/ HTTP/1.1" 404 187 "https://www.test.com/blog/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.87 Safari/537.36"
Checking the logs, it seems to look into the wrong place, which is /etc/nginx/html/index.php, but if I add the alias in the php location block, then the homepage will stop working as well, which gets me a bit confused.
Currently, I'm not quite sure if the problem is the alias in the blog block ( nginx recommends not using alias together with try_files, apparently due to a bug ), or anything else. If it is indeed the alias directive, I'll try to add root, together with some rewrite rules to avoid modifying the file structure.
It really took me more than it should to figure it out, and still can not actually understand what might be the issue here.
UPDATE 1 :
This is extremely odd. With the following blog config, separating the php location from the blog, the WP posts are accesible, but not the homepage and admin, returning 404:
location /blog {
alias /var/www/html;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
alias /var/www/html;
try_files $uri =404;
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_index index.php;
fastcgi_pass blog-test:9000;
fastcgi_split_path_info ^(/blog)(/.*)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
UPDATE 2 :
Now this works ( the blog and the posts can be accesed ) , but the admin seems to be going into a redirect loop:
location /blog {
alias /var/www/html;
index index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
}
location ~ \.php$ {
alias /var/www/html;
try_files $uri $uri/ /index.php?$args;
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_index index.php;
fastcgi_pass blog-test:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
UPDATE 3 & FIX :
This apparently works, but the config seems to be really unpleasant and unoptimised. If I try to nest the php location into the blog block, then the php files will download instead of rendering. If I try to use alias instead of root, some pages will not show resulting in 404. In any case, this seems to be functional:
root /var/www/html;
index index.php;
location /blog {
rewrite ^/blog(.*)$ /$1 break;
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
rewrite ^/blog(.*)$ $1 break;
fastcgi_pass blog-test:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_split_path_info ^(/blog)(/.*)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
Outside the /blog dir you might also have php files. There should be a separate block for the wordpress install that has the same settings, but then for the blog dir.
This could be the block for wordpress (tested in Docker)
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
location ~ \.php$ {
fastcgi_split_path_info ^/blog/(.+\.php)(/.+)$;
fastcgi_pass wp:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
So your complete nginx config would look like this:
server {
listen 80;
listen [::]:80;
server_name www.test.com;
root /var/www/html;
index index.php;
location ^~ /media {
alias /var/local/test/static/media;
}
location ^~ /icons {
alias /var/local/test/static/icons;
}
# This is for Wordpress
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
location ~ \.php$ {
fastcgi_split_path_info ^/blog/(.+\.php)(/.+)$;
fastcgi_pass wp:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
# This is for php files in the root. If there is no php to be parsed there you could leave these blocks out.
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass wp:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
UPDATE: I added a root and index property to the nginx server config.
UPDATE 3 & FIX :
This apparently works, but the config seems to be really unpleasant and unoptimised. If I try to nest the php location into the blog block, then the php files will download instead of rendering. If I try to use alias instead of root, some pages will not show resulting in 404. In any case, this seems to be functional:
root /var/www/html;
index index.php;
location /blog {
rewrite ^/blog(.*)$ /$1 break;
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
rewrite ^/blog(.*)$ $1 break;
fastcgi_pass blog-test:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_split_path_info ^(/blog)(/.*)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param PATH_INFO $fastcgi_path_info;
}

Laravel, Docker and Nginx: 404 for all files in /public folder

I created a new project in Laravel 7 by using docker-compose 3.3 and Nginx 1.17.
The enpoints created on Laravel works well, the problem comes when I try to access to whatever static asset in the '/public' folder. I tried with .css and .js files and also with the robots.txt but Laravel returns a 404 not found error.
This is my nginx.conf (taken from https://laravel.com/docs/7.x/deployment#nginx)
events {
}
http {
server {
server {
listen 80;
server_name localhost;
root /var/www/app/public;
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass app:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
}
I tried different fixes found on forums and on Stackoverflow, like recompiling files, clear the cache... But nothing works.
Can you spot the problem?
can you try the following config:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}

Nginx is downloading PHP files when trying to remove the extension

So, basically my current configuration is like below. Whenever I try and request the URL without the PHP extension it downloads it instead?
server {
listen [::]:80;
root myDirectory;
index index.php index.html;
server_name myDomain;
location / {
try_files $uri $uri/ $uri.php $uri.php$is_args$query_string =404;
}
location ~\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
}
location ~ /\ ht {
deny all;
}
}
I've already done what a lot of other answers in other questions have suggested like editing the php7.1-fpm php.ini file:
cgi.fix_pathinfo=0
Any ideas?
Your current use of try_files will not work. The file elements of the try_files statement are processed within the same location block, which is the wrong location for PHP files. See this document for more.
There are a number of solutions, but you could use a named location to perform an internal rewrite if the script file is found to exist.
For example:
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
if (-f $document_root$uri.php) { rewrite ^ $uri.php last; }
return 404;
}
location ~ \.php$ {
try_files $uri =404;
include snippets/fastcgi-php.conf;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
}
See this caution on the use of if.

nginx 403 forbidden error + mac + laravel

Here is my nginx's content.
My current access url is http://localhost/lampi/, and I am receiving response 403 forbidden.
My server's documentroot is /Library/WebServer/Documents/.
When I access http://localhost/, it shows ok. I can also see the index.html page's content.
I don't know what the matter is. I have checked top 10 pages in stackoverflow.
server {
server_name localhost;
access_log /var/log/nginx/nginx.host.access.log main;
root /Library/WebServer/Documents/;
location / {
#root html;
index index.html index.htm index.php;
}
location /lampi {
#autoindex on;
if (!-e $request_filename){
rewrite ^/lampi/(.*)$ /lampi/index.php?s=$1 last;
}
}
location ~ \.php$ {
include /usr/local/etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /Library/WebServer/Documents/lampi/$fastcgi_script_name;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_param HTTP_PROXY "";
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
#location /images/ {
# root /usr/local/var/www;
#}
}
There are three potential problems with your configuration file.
The use of if (!-e $request_filename) is causing problems because it checks for the existence of directories, and you should probably be using try_files anyway (see this document for details):
location /lampi {
try_files $uri $uri/ #lampi;
}
location #lampi {
rewrite ^/lampi/(.*)$ /lampi/index.php?s=$1 last;
}
The value of SCRIPT_FILENAME adds an extra /lampi into the pathname. Use either of (both evaluate to the same value in this case):
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
For example:
location ~ \.php$ {
try_files $uri =404;
include /usr/local/etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
The location ~ [^/]\.php(/|$) block is confusing. Normally it is adequate to use either location ~ \.php$ or something like location ~ [^/]\.php(/|$) depending on whether or not your application uses PATH_INFO. Delete the block you are not using. See this document for details.

Windows Nginx Inuput File not specified error

PROBLEM: Problem i am facing is when I open localhost/phpmyadmin it gives me "No Input file Specified"
I am trying to configure Nginx and php on Windows.
Nginx installed path is E:\server\nginx
php installed path is E:\server\php
PhpMyAdmin installed path is E:\phpmyadmin\
My Root directory path is E:\server\www
Following code is from nginx.conf file.
server {
listen 80;
server_name localhost;
root E:\server\www;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location /phpmyadmin/ {
alias E:/server/phpmyadmin/;
try_files $uri /phpmyadmin/index.php =404;
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}
I am running nginx and php from command prompt
cd `E:\server\nginx\
nginx.exe`
cd E:\server\php\
php-cgi.exe -b 127.0.0.1
server runs fine. I can access localhost/
I can also access any .php file from www folder.
PROBLEM: Problem I am facing is when I open localhost/phpmyadmin it gives me "No Input file Specified"
Please tell me I am doing wrong. Thank you in advance.
Give this an attempt:
server {
listen 80;
server_name localhost;
root E:\server\www;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location /phpmyadmin {
alias E:/server/phpmyadmin/;
index index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
}

Categories