Nginx setup for laravel project, which has routes with ".php" endings - php

I'm trying to setup my Laravel project using Nginx. My /etc/nginx/conf.d/default.conf is:
server {
listen 80;
index index.php index.html;
root /var/www/public;
location / {
try_files $uri /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
I have a problem with routes with ".php" endings, e.g.
Route::get('modules.php', 'ModuleController#index');
Instead of going to index.php and looking there the route, the server tries to open file modules.php, which doesn't exist.
I know, that problem in nginx settings, but I don't have experience with it, so I can't fix it myself.

Look through this page https://www.digitalocean.com/community/tutorials/how-to-install-laravel-with-an-nginx-web-server-on-ubuntu-14-04
Here you can find suitable configuration and description.
For example:
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 app:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Add a try_files statement to the second location block.
For example:
location / {
try_files $uri /index.php?$args;
}
location ~ \.php$ {
try_files $uri /index.php?$args;
fastcgi_pass app:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Related

Nginx change the root location to wordpress site

Currently I have a Wordpress site under /var/www/html/wordpress and other php projects under /var/www/html/projects. I want my root location to point to wordpress one. Here is my current nginx config:
server {
listen 80 default_server;
server_name _;
set $yii_bootstrap "index.php";
root /var/www/html;
client_max_body_size 2M;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /projects/inspection/ {
root /var/www/html;
try_files $uri /index.php$is_args$args;
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_index index.php;
fastcgi_pass 127.0.0.1:9000;
}
location /projects/ {
root /var/www/html;
index index.html $yii_bootstrap;
try_files $uri $uri/ /$yii_bootstrap?$args /index.php$is_args$args;
}
location / {
root /var/www/html/wordpress;
try_files $uri $uri/ =404;
index index.php index.html index.htm;
}
location ~ \.php{
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_pass php-fpm;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.html;
location = /40x.html {
}
}
But it returns 404. I tried to put alias instead and it returns 403 forbidden. How should I handle this?

Laravel Nginx Query String Parameters

I'm using Laravel 5.2 and Nginx and works fine on my development server.
For example:
http://example.com/results?page=2
Development Server
LengthAwarePaginator::resolveCurrentPage(); // returns 2
Paginator::resolveCurrentPage(); // returns 2
$request->input('page'); // returns 2
But in production server
LengthAwarePaginator::resolveCurrentPage(); // returns 1
Paginator::resolveCurrentPage(); // returns 1
$request->input('page'); // returns null
Production Server Configuration
server {
listen 80 ;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html/laravel/public;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name example.com www.example.com;
location / {
try_files $uri $uri/ /index.php;
}
location #rewrite {
rewrite ^(.*)$ /index.php?_url=$1;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php$is_args$args;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include /etc/nginx/fastcgi_params;
}
}
Development Server Configuration
server {
listen 80;
server_name example.com;
client_max_body_size 50M;
root /var/www/html/laravel/public;
index index.html index.htm index.php;
try_files $uri $uri/ #rewrite;
add_header 'Access-Control-Allow-Origin' '*';
location #rewrite {
rewrite ^(.*)$ /index.php?_url=$1;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php5.6-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "upload_max_filesize=52428800 \n post_max_size=53477376 \n memory_limit=536870912";
fastcgi_param APPLICATION_ENV development;
fastcgi_split_path_info ^(.+\.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;
include fastcgi_params;
}
}
What changes should I make in server nginx configuration?
Any help appreciated!
Resolved!
I needed to make some corrections as there were two fastcgi_pass in location ~ \.php$ {} so had to change
location ~ \.php$ {
try_files $uri $uri/ /index.php$is_args$args;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include /etc/nginx/fastcgi_params;
}
To
location ~ \.php$ {
try_files $uri $uri/ /index.php$is_args$args;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include /etc/nginx/fastcgi_params;
}
and modify try_files in location / {} hence had to change
location / {
try_files $uri $uri/ /index.php;
}
To
location / {
try_files $uri $uri/ /index.php?$query_string;
}

NginX Configuration Downloading index.php

I have the following nginx configurations.
mydomain.com /etc/nginx/sites-available/default
DNS Settings - Record Type:ANAME, Name:#, IP:1.2.3.4
server {
listen 80;
listen [::]:80 ipv6only=on;
root /var/www/html/mydomain/public;
index index.php index.html;
server_name mydomain.com;
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;
}
}
stats.mydomain.com /etc/nginx/sites-available/piwik
DNS Settings - Record Type:ANAME, Name:stats, IP:1.2.3.4
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name stats.mydomain.com;
root /var/www/piwik;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
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;
}
}
My primary domain works perfectly and processes the php correctly. However, my subdomain displays the raw code/index.php in the browser.
What am I missing?
Any help would be appreciated.

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;
}
}
}

Nginx index module not point to index.php

My problem is access main site: example.com not point to index.php, i have to manually enter full url example.com/index.php.
Subdomain sub01.example.com working well, automatic point to index.php.
I've 3 site as bellow, please help to check
main site: example.com
subdomain: sub01.example.com, sub02.example.com
and this is my configure:
server {
listen 80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
server {
listen 80;
server_name example.com;
# root directive should be global
root /var/www/example.com/;
index index.php;
access_log /var/www/example.com/logs/nginx_access.log;
error_log /var/www/example.com/logs/nginx_error.log;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
server {
listen 80;
server_name sub01.example.com www.sub01.example.com;
# root directive should be global
root /var/www/sub01.example.com/;
index index.php;
access_log /var/www/sub01.example.com/nginx_access.log;
error_log /var/www/sub01.example.com/nginx_error.log;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
server {
listen 80;
server_name sub02.example.com www.sub02.example.com;
# root directive should be global
root /var/www/sub02.example.com/;
index index.php;
access_log /var/www/sub02.example.com/logs/nginx_access.log;
error_log /var/www/sub02.example.com/logs/nginx_error.log;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
I'm sorry.
This configuration was fine.
Error come from cloudflare DNS pointer config.

Categories