NGINX with slashes after index.php - php

I am trying to get URLs that looks like https://my.site.com/index.php/user/login to work on nginx.
From what I understand about server blocks, nginx thinks that index.php/user/login are either a path or a directory, so it doesn't process them correctly and dies with a 404.
My server block's try file looks like this:
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php$is_args$args;
# Enable CORS
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length';
}
What am I missing? All I need nginx to do is "aha! index.php is right there; my job is done", then the framework will realize that "ha! the whole URL is https://my.site.com/index.php/user/login; that means they want me to run the user controller, then load the login action" and that's it

I use Laravel which defaults to similar rewrites (no index.php in the url)
Here is a final structure that works well,
Explained here
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/laravel/public;
index index.php index.html index.htm;
server_name server_domain_or_IP;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =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;
}
}

Related

How to make a server block configuration file to run a CakePHP app on Nginx server?

Since I'm new to DigitalOcean and to Nginx server I don't have an idea what I am doing exactly
Having said that here's issue I'm getting
I've put the CakeApp folder inside /var/www/html folder and also I added an info.php page into webroot folder of my CakeApp to check if I can access it
When I go to http://my_ip/CakeApp it redirects to http://my_ip/CakeApp/login page and give a 404, but when I access the info.php file by going to http://my_ip/CakeApp/info.php it works and returns the PHP info page
here is the sever block file
server {
listen 80;
listen [::]:80;
server_name app.cake.com;
return 301 http://app.cake.com$request_uri;
root /var/www/html/CakeApp/public/webroot;
index index.php;
location /CakeApp/webroot {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
}
So I want to get the login page when I go to http://my_ip/CakeApp
Found a fix.
server {
listen 80;
root /var/www/html/CakeApp/webroot;
index index.php index.html index.htm index.nginx-debian.html;
server_name example.com;
location / {
rewrite ^(.+)$ $1 break;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}

Nginx make any URL work with or without a trailing slash

I want any directory to serve up either index.html or index.php – whether a trailing slash was given or not.
Right now my directories will load when a trailing slash is given, but not without.
# Works
localhost/mydirectory/
# Does not work
localhost/mydirectory
I just want it to work either way. Here is my nginx server block:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www;
index index.php index.html index.htm;
server_name localhost;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Your help is appreciated. Thanks!

nginx - Friendly URL's in directory

I move my Friendly URL's from Apache to nginx and I have a problem. I want to Friendly URL's only works within the subdirectory sgforum.
In PHP, I receive the addresses as: 127.0.0.1/sgforum/index, 127.0.0.1/sgforum/member etc.
When I go on 127.0.0.1/sgforum/ - it works, but when I give member (127.0.0.1/sgforum/member), or index, it downloads a file to my computer, instead of opening with php.
This is my /etc/nginx/sites-available/default file:
server {
listen 80 default_server;
#listen [::]:80 default_server;
root /home/ariel/workspace;
index index.php index.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# FRIENDLY URLS
location /sgforum/ {
if (!-e $request_filename){
rewrite ^/sgforum/(.*)$ /sgforum/index.php break;
}
}
location ~ /\.ht {
deny all;
}
}
I changed it, and finally works as it should.
# FRIENDLY URLS
location /sgforum/ {
try_files $uri $uri/ /sgforum/index.php;
}
you have to set location for member folder
try change
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

Nginx not loading index.php

I'm using nginx and php5-fpm in my ubuntu pc, my site is not loading in browser i have configured everything, but i'm getting 500 internal server error in browser console when i was run my index.php.
This is my code (etc/nginx/sites-available/default)
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html/Inwiter;
index index.php index.htm index.html;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php;
#try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
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;
}
}
Few days back i also faced this kind of issue (not exactly this issue) that time i was added allow accessToken in my configuration file then it was fine for me.
add_header Access-Control-Allow-Origin *;
As the log you provided, nginx still try to use fastcgi://127.0.0.1:9000 as fastcgi_pass .
Did you restart nginx or reload configuration after modification ?
Besides, please check configuration file in /etc/php5/fpm/pool.d/.
There must be a line with listen = /var/run/php5-fpm.sock.
I recommend you to remove ipv6only=on , and try following sample configuraion:
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_intercept_errors on;
try_files $uri =404;
}

Laravel's NotFoundHttpException won't dissapear in Nginx

I'm using Nginx with Laravel and I keep getting a NotFoundHttpException error.
Nginx won't log anything. I've also tried to add die('Test'); inside my public/index.php file, but that won't be dumped as well.
My Nginx server block looks like this:
server {
listen 0.0.0.0:80;
server_name test.docker.dev;
root /usr/share/nginx/html/docs/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
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;
}
access_log /var/log/nginx/default_access.log;
error_log /var/log/nginx/default_error.log;
}

Categories