I install nginx with help of this site
and when l go to http://localhost/index.php it gives me empty screen. This my default:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.php index.nginx-debian.html;
server_name localhost;
location / {
try_files $uri $uri/ /index.html;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# include snippets/fastcgi-php.conf;
#
# # 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;
}
}
What's wrong with it?
try this for php location:
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; }
then reload nginx and php-fpm(if you get a bad-gateway is a php-fpm error(reload it))
Related
I have two applications:
a wordpress site at /var/www/html/wordpress
a symfony application at /var/www/html/symfony.
The wordpress application is running as the main domain (domain.com).
I want to achieve the following behavior:
a user visits URL domain.com/example1
nginx redirects to the Symfony route /example1.
With my current config nginx already redirects to the Symfony app.
It loads the wordpress site and its admin dashboard correctly.
Issue:
nginx returns the Symfony home page (/) instead of /example1.
The URLs domain.com/example1 and domain.com/example2 loads the Symfony homepage instead of its corresponding route created in the Symfony app.
My nginx configuration:
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name domain.com;
server_tokens off;
root /var/www/html/wordpress;
index index.php index.html index.htm;
index index.html index.htm index.php;
client_max_body_size 500M;
# charset utf-8;
location / {
# CUSTOM
satisfy any;
charset utf-8;
allow 1.1.1.0/32;
deny all;
try_files $uri $uri/ /index.php?$query_string;
}
### start test
location ^~ /example1 {
satisfy any;
allow 1.1.1.0/32;
deny all;
index index.php;
alias /var/www/html/symfony/current/public/;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location ^~ /example2 {
satisfy any;
allow 1.1.1.0/32;
deny all;
index index.php;
alias /var/www/html/symfony/current/public/;
try_files $uri $uri/ /index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location ~ ^((?!\/example1).)*$ { #this regex is to match anything but `/example1`
satisfy any;
allow 1.1.1.0/32;
deny all;
index index.php;
root /var/www/html/wordpress;
try_files $uri $uri/ /index.php?$request_uri;
#try_files $uri $uri/ /index.php?do=$request_uri;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
### end test
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/domain.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
# CUSTOM
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
location ~ /\.(?!well-known).* {
deny all;
}
You're potentially overriding the fastcgi_* parameters with the defaults in the file fastcgi_params after setting them.
Instead of:
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# the following line loads defaults from file `fastcgi_params`
include fastcgi_params;
Move the include directive after fastcgi_pass and fastcgi_split_path_info but before setting SCRIPT_FILENAME like this:
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
include fastcgi_params;
# the following fastcgi_* parameters override the defaults in file `fastcgi_params`
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
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;
}
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.
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.
I have problem with my website, all assets(css,js,fonts) not loaded.
My configuration :
server {
listen 80;
server_name shop.dev;
index index.php index.html index.htm;
access_log /var/log/nginx/test.dev.access.log;
error_log /var/log/nginx/test.dev.error.log;
location / {
#index app_dev.php;
#try_files $uri /app_dev.php?$args;
try_files $uri /app_dev.php$is_args$args;
}
location ~ .php$ {
root /home/vagrant/Workspace/shop/web;
index index.html index.htm index.php;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param APPLICATION_ENV development;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
}
sendfile off;
}
I use vagrant machine. Exist a solution? Help me please!!