My client's website use Wordpress. The permalink is set to /%postname%.php. See there is .php. So an URL would be look like http://www.example.com/article-url.php.
I am using Nginx and PHP-FPM. That permalink setting did not work as I got File not found error.
Here is my Nginx server block
server {
listen 80;
root /var/www/html/example.com;
index index.php index.html index.htm;
error_log /var/log/nginx/example.com.error.log;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
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;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
}
Here is the error log says FastCGI sent in stderr: "Primary script unknown" while reading re sponse header from upstream ...
How to correct this?
I've found the solution here.. So what I need to do is to add try_files $uri $uri/ /index.php?$args; under php location
My server block now:
server {
listen 80;
root /var/www/html/example.com;
index index.php index.html index.htm;
error_log /var/log/nginx/example.com.error.log;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?$args;
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;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
}
Related
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;
}
}
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;
}
This is my config
server {
listen 80;
listen [::]:80;
server_name mysite.com;
root /var/www/site/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files \$uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)\$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}
}
The root of my site is accessible, all other routes are returning a 404 error.
Thanks in advance.
Cheers.
A few changes ... I run php 7.1 ...
location... try /index.php?$query_string; as in the example below...
server {
listen 80;
listen [::]:80;
server_name mysite.com;
root /var/www/site/public;
index index.php index.html;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Make sure your PHP-FPM runs same user as nginx
i.e. sudo vim /etc/php/7.1/fpm/pool.d/www.conf
...
user = nginx
group = nginx
...
listen.owner = nginx
listen.group = nginx
...
Then sudo systemctl restart php7.1-fpm.service
This is my configuration.
server {
listen 80;
root /home/user/projects/apple/public;
index index.php index.html index.htm;
server_name apple.dev;
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:/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I am not sure what the difference is, but I deleted the config file and made a copy of the default config, made changes to the lines I needed to. (i.e. server_name, root)
And it started working.
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 developed my CI app and tested it on over 4 Apache server and it's working fine. But aftaer i upload it int to the new server which is Nginx, it has problem with urls. I Googled a lot and approximately did every thing but there was not result. Now here's the problem:
Due to project structure and scale i need every url to be like this:
http://example.com/myappname/controller/action
ex: http://example.com/myappname/auth/login
But it's not working. What is working is:
http://example.com/myappname/index.php?/auth/login
which is not what i want.
The nginx configuration is:
server {
listen 80;
listen [::]:80;
root /var/nginx/www/mydomain.com/www;
index index.php index.html index.htm;
server_name mydomain.com www.mydomain.com;
location / {
try_files $uri $uri/ /index.php?/$request_uri;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
# With php5-fpm:
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
location ~* \.(png|jpg|jpeg|gif|ico|ttf|woff)(\?ver=[0-9.]+)?$ {
expires 1w;
}
location ~* \.(css|js|html)(\?ver=[0-9.]+)?$ {
expires 1w;
}
}
Your codeIgniter config.php contains the following information:
$config['base_url'] = "http://domain.tld/";
$config['index_page'] = "";
$config['uri_protocol'] = "REQUEST_URI";
And here is the nginx rewrite rule,
server {
server_name domain.tld;
root /var/www/codeignitor;
index index.html index.php;
# 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 or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}
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;
}
}
I had the same problem.
I have a website in
http://website.com
and the codeigniter folder in
http://website.com/partners.
I just change the nginx configuration to have a location specifically for this codeigniter folder.
I add it like this.
location / {
try_files $uri $uri/ /index.php;
}
location /partners {
try_files $uri $uri/ /partners/index.php;
}
As for your case. I think this setting should work like below :
server {
listen 80;
listen [::]:80;
root /var/nginx/www/mydomain.com/www;
index index.php index.html index.htm;
server_name mydomain.com www.mydomain.com;
location / {
try_files $uri $uri/ /index.php;
}
#ADD THIS LINE
location /myappname {
try_files $uri $uri/ /myappname/index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
# With php5-fpm:
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
location ~* \.(png|jpg|jpeg|gif|ico|ttf|woff)(\?ver=[0-9.]+)?$ {
expires 1w;
}
location ~* \.(css|js|html)(\?ver=[0-9.]+)?$ {
expires 1w;
}
Make sure to test that everything is working by running sudo nginx -t and then restart nginx. Hopefully it will work.