I am trying to implement nginx fastcgi caching with my laravel site but I'm getting a blank white page.
I've checked laravel's and nginx's logs and they show no errors or even access logs but the cache does get stored in the directory it's supposed to.
I have tried the same caching directives with a wordpress install on the same server which works flawlessly. I'm just wondering if there are specific nginx cache directives that need to be used for laravel and also if anyone can recommend a better alternative for caching for laravel.
here's my server conf:
fastcgi_cache_path /tmp/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
server {
listen 80;
listen [::]:80;
server_name site.com www.site.com;
root /var/www/site/public;
index index.php index.html index.htm index.nginx-debian.html;
set $no_cache 0;
charset utf-8;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
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/myapp-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_cache MYAPP;
fastcgi_cache_valid 200 60m;
fastcgi_cache_methods GET HEAD;
add_header X-Fastcgi-Cache $upstream_cache_status;
fastcgi_cache_bypass $no_cache;
fastcgi_no_cache $no_cache;
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;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /\.ht {
deny all;
}
}
Related
I am trying to setup nginx cache for Laravel application in order to cache Glide generated urls.
My host config template is as follows:
fastcgi_cache_path /tmp/nginx/cache levels=1:2 keys_zone=BACKEND:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
server {
listen 443 ssl http2;
server_name ${URL};
root /var/www/backend/public;
index index.php;
ssl_certificate ${SSL_CERTIFICATE};
ssl_certificate_key ${SSL_CERTIFICATE_KEY};
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /favicon.ico { access_log off; log_not_found off; }
location /robots.txt { access_log off; log_not_found off; }
location ~ \.php$ {
fastcgi_pass ${CONTAINER}:9000;
try_files $fastcgi_script_name =404;
fastcgi_index index.php;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 600;
fastcgi_cache BACKEND;
fastcgi_cache_valid 200 1m;
fastcgi_cache_bypass $no_cache;
fastcgi_no_cache $no_cache;
fastcgi_cache_use_stale error updating timeout invalid_header http_500;
fastcgi_cache_lock on;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
error_page 404 /index.php;
#Cache everything by default
set $no_cache 0;
#Don't cache POST requests
if ($request_method = POST)
{
set $no_cache 1;
}
}
The cache seems to be working but the problem is that every glide generated image returns with status of 404, even though images are displayed and rendered correctly.
What could be causing such behaviour?
I am setting up new laravel project with nginx server configuration ,in sites-available and sites-enabled files i have setup the laravel project details and i added host name in etc/hosts location also,when i hit url(http://hrmcc.test/) in browser i am facing file not found message in browser body,can you please help me to fix this issue
error in log
2022/08/17 13:24:34 [error] 4801#4801: *4 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: hrmcc.test, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.4-fpm.sock:", host: "hrmcc.test"
sites-available && sites-enabled
server {
listen 80;
server_name hrmcc.test;
root /home/deploy/hrm_v3.2/public;
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
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/hrmc-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
}
Try to use the default Laravel NGINX configuration.
server {
listen 80;
listen [::]:80;
server_name example.com;
root /home/deploy/hrm_v3.2/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
This should work. Make sure that your php-fpm socket exists on the location in your configuration.
Also make sure to test your configuration by running nginx -t.
I have a nginx block for a wordpress installation, and a subdirectory which redirects to an app I have running. I want .php urls that go to the subdirectory to not be evaluated by fastcgi, since the app has it's own special system for evaluating them. However, php URLs still go to the fastcgi block and return a 404.
Nginx configuration:
server {
listen 0.0.0.0:443 http2 default_server;
listen [::]:443 http2 default_server;
ssl on;
ssl_certificate /etc/nginx/certificate.pem;
ssl_certificate_key /etc/nginx/certificate.key;
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; allow all; }
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
root /var/www/wordpress;
index index.html index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /my_app {
proxy_pass http://localhost:8081/;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I want /index.php to be evaluated by fastcgi.
I want /my_app/index.php to be proxy_pass'd to my app.
I have setup sub domain routing with nginx and laravel for my development site, but for some reason when i access the site with www.lunch.test it works but when i access the site with companyA.lunch.test it doesn't work.
Below is my nginx lunch.conf file. I will be on stand by for solution.
listen 80;
server_name www.lunch.test lunch.test *.lunch.test;
root /var/www/html/lunch/public;
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log on;
error_log /var/www/html/lunch/storage/logs/error.log error;
access_log /var/www/html/lunch/storage/logs/access.log main;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
}
Thanks to all viewers, i was able to resolve the issue by mapping the domain name to the ip address in /etc/hosts file like so,
xxx.xxx.xxx.xxx companyA.lunch.test
I'm using laravel in my centos 6.7 and webuzo.
I can run my script on apache before, but when i mirgate it to nginx, it won't run.
I think the problem is, i can't use this nginx conf :
server {
listen 80;
server_name myapp.localhost.com;
root /srv/smile/public;
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
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/myapp-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /\.ht {
deny all;
}
}
Everytime i put this config in nginx.conf, nginx service won't restart. What could be wrong with this conf ?