I am working on a laravel V5.1.11 site which is hosted on AWS EC2 ubuntu with ngnix server. I successfully setup the site but my inner page are not working.
Config is:
server {
listen 82;
server_name www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 83;
server_name www.example.com;
root /home/in4matic/example-website-dev/public;
location / {
index index.php;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
#fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
How can I fix that.
Moving laravel app from apache to nginx doesn't require many modification, but because laravel uses .htaccess file for url rewrite which won't work in nginx, you have to modify nginx config file so nginx can rewrite url. here is the example of my config file :
server {
listen 80;
server_name your-website.com;
# note that these lines are originally from the "location /" block
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000 # Keep this as per your old config;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I have been using this config for all of my laravel app.
and make sure that Storage directory has proper permission.
Related
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;
}
I'm setting up an Nginx + PHP web server on AWS Linux 2. It is a fresh install with nginx and PHP7.4 installed. Below is the virtual host config file in nginx.
I need to redirect all the traffic to index.php because it is a Single Page App.
When I go to www.xxx.com/index.php, the PHP page renders fine (so PHP is definitely running).
When I go to www.xxx.com/login/, the browser prompts for download of the index.php file instead of executing it.
Can anyone please help? (I've tried to clear my browser cache).
/etc/nginx/conf.d/myapp.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/vhosts/app.userback.io/frontend/;
index index.php index.html index.htm;
server_name www.myapp.com;
location / {
try_files $uri $uri/ /index.php =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass php-fpm;
}
}
Can you try:
location ~ \.php$ {
try_files $uri /404.html;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;// change it if not valid
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
I've found the answer here: NGINX try_files does not pass to PHP.
It turns out it is the order of the location plus removing the 404 to make the redirect internal.
I'v configured Ngnix server on CentOs 7 and it is working find with html but when I'm trying to open .php file it is downloaded.
I'm using Php7.2.
Here is my configuration(I'm using 123.123.123.123 instead my real ip):
server {
listen [::]:80 default_server ipv6only=on;
listen 123.123.123.123:80 default_server;
root /var/www/example.com/public_html;
index index.php index.html;
server_name 123.123.123.123;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
So when I'm trying to access to 123.123.123.123/test/index.php I'm geting file download.
How could I force ngnix to execute php files?
You also need fastcgi_pass:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
I have a development box that each user has a www folder in the home dir. NGINX is hosting those dirs from http://IP ADDRESS/USERNAME. And this works great. I want to get PHP working in the same fashion.
/etc/nginx/sites-available-default:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
#root /usr/share/nginx/html;
#index index.html index.htm;
# Make site accessible from http://IP ADDRESS/
server_name IP ADDRESS;
#location ~ ^/(.+?)(/.*)?$ {
location ~ ^/(.+?)(/.*)?$ {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
alias /home/$1/www$2;
index index.html index.htm index.php;
include /etc/nginx/php.fast.conf;
autoindex on;
}
php.fast.conf file:
location ~ \.php$ {
fastcgi_split_path_info ^(.+?\.php)(/.*)?$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
# fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /home/$1/www$2$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
As you can see I have tried a few variations but seem to continue to receive the following error in the log: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client.
When attempting to render a simple PHP info page The page displays "file not found"
Other info:
Server = ubuntu 14.x
Latest Nginx
Digital Ocean Droplet
Thanks in advance.
server {
listen 80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location ~* \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $fastcgi_script_name =404;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi_params;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location ~* ^/(.+?)/www(/.*|/|)$ {
root /home;
index index.php index.html;
fastcgi_pass unix:/var/run/php5-fpm.sock;
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi_params;
}
location ~* ^/(.+?)(/.*|/|)$ {
index index.php index.html;
try_files $uri $uri/ #home;
}
location #home {
rewrite ^/([^/]+?)$ /$1/www/ last;
rewrite ^/(.+?)(/.*|/)$ /$1/www$2 last;
}
}
The cons of this config, you can't use URI like /<anything_you_want>/www.
Maybe it helps. But it's weird, non-optimized and ugly config.
I would like to serve 2 PHP(Symfony) applications under the same domain, with a URL prefix for one of the apps.
So www.mydomain.com/ should serve the first app while www.mydomain.com/secondapp should serve the second one.
I ended up with this config which doesn't work :
server {
server_name mydomain.com www.mydomain.com;
location / {
root /var/www/first-app/web;
try_files $uri /app.php$is_args$args;
}
location /secondapp {
root /var/www/second-app/web;
try_files $uri /app.php$is_args$args;
}
location ~ ^/(app|app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
When I put the root directive for the first app under the last location, it works for the first app but the second can't be served.
Should I create a second server directive and use sub-domain rewriting instead?
Thanks!
You should use alias instead of root in your locations. If you use root, the path that is matched in the location is added to the root path.
So I finally found the solution: my nginx conf:
server {
listen 80;
server_name www.mydomain.com;
root /var/www/first-app/web;
location ~ ^/secondapp(/.*)$ {
alias /var/www/second-app/web;
try_files $1 #app;
}
location #app {
expires off;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/second-app/web/app.php;
fastcgi_param SCRIPT_NAME /secondapp/app.php;
fastcgi_param REQUEST_URI /secondapp$1;
}
location / {
try_files $uri /app.php$is_args$args;
}
location ~ ^/(app|app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
error_log /var/log/nginx/app_error.log;
access_log /var/log/nginx/app_access.log;
}