We have added expire header in /etc/nginx/site-available/default, but not reflecting in gtmatrix/google speed, we also check with curl website.
Please check our code below,
server {
root /var/www/html;
location / {
index index.php index.html;
try_files $uri $uri/ #handler;
}
location /. {
return 404;
}
location #handler {
rewrite / /index.php;
}
location ~ .php/ {
rewrite ^(.*.php)/ $1 last;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
if (!-e $request_filename) {
rewrite / /index.php last;
}
include snippets/fastcgi-php.conf;
# With php7.0-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
fastcgi_read_timeout 20000;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#fastcgi_param PHP_VALUE "memory_limit = -1";
}
location ~* .(js|jpg|jpeg|gif|png|css|tgz|gz|rar|bz2|doc|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|txt|woff|woff2|svg)$ {
expires 30d;
add_header Pragma "public";
add_header Cache-Control "public";
}
}
Thanks in advance!
Can you try with an additional backslash?
location ~* \.(js|jpg|jpeg|gif|png|css|tgz|gz|rar|bz2|doc|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|txt|woff|woff2|svg)$ {
expires 30d;
add_header Pragma "public";
add_header Cache-Control "public";
}
Also, you may want to split the block in order to use gzip off when it makes sense.
Best regards,
Related
I have been trying to setup a directory to use a seperate root directory (or alias).
location ~ \.php$ # root location
{
try_files $uri #php;
include /etc/openresty/fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
location ^~ /sub2/
{
rewrite_log on;
alias /var/www/sub2/;
location ~ \.php$
{
try_files $uri #ono_php;
include /etc/openresty/fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
}
try_files $uri #ono_main_cache;
}
location #ono_php
{
rewrite ^/sub2/(.+)$ /index.php?request=$1 last;
}
location #ono_main_cache
{
if ( $http_accept_encoding !~ gzip )
{
rewrite ^/sub2/(.*)$ /index.php?request=$1 last;
}
if ( $query_string )
{
rewrite ^/sub2/(.*)$ /index.php?request=$1 last;
}
try_files /var/www/sub2/data/cache/html$uri.html.gz #ono_php;
add_header Content-Encoding gzip;
gzip off;
default_type text/html;
}
There is more in the overall nginx configuration for the domain, but I've only included what is relevant.
To overview, I want the /sub2/ to use the root/alias /var/www/sub2/, rather than the default /var/www/site/. That works correctly. However, all URLs that don't load a file should be redirected to index.php as in the #ono_php block. However, they use the first location block listed (with # root location appended to it). So if I load the URL:
https://example.com/sub2/contact
It loads the same URL as:
https://example.com/contact
How can I get the block #ono_php to use the location ~ \.php$ defined inside location ^~ /sub2/?
There were a number of issues. The main being I set the root/alias to /var/www/sub2 and when requests were made, they had that directory appended from the URL, e.g. the path requested was /var/www/sub2/sub2/url - where sub2/url is the URL. Changing the root to /var/www fixed this.
location ^~ /sub2
{
root /var/www;
try_files $uri $uri/ #ono_main_cache;
location ~ \.php$
{
include /etc/openresty/fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
}
}
location #ono_php
{
rewrite ^/sub2/(.*)$ /sub2/index.php?request=$1 last;
}
location #ono_main_cache
{
if ( $http_accept_encoding !~ gzip )
{
rewrite ^/sub2/(.*)$ /sub2/index.php?request=$1 last;
}
if ( $query_string )
{
rewrite ^/sub2/(.*)$ /sub2/index.php?request=$1 last;
}
try_files /sub2/data/cache/html$uri.html.gz #ono_php;
add_header Content-Encoding gzip;
gzip off;
default_type text/html;
}
I have this NGINX conf file,
If I load any root url, it will show my default.jpg image,
but all of my mail images are stores in subfolders, eg:
/missing.jpg // works
/images/mising.jpg // shows nginx 404 not /default.jpg
Here is my nginx conf.
Note: either I can use: /default.jpg or /index.php which outputs the same image..
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php;
server_name _;
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
add_header Cache-control "public";
access_log off;
expires 365d;
}
location / {
#try_files $uri $uri/ default.jpg;
try_files $uri /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
I solved this by adding the try_files to the image cache block.
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
add_header Cache-control "public";
access_log off;
expires 365d;
try_files $uri /index.php$is_args$args;
}
Currently, I'm trying to set up a Magento 2 site that will run with http://www.example.com/store/ as its home page, but it seems that Magento's router doesn't know about the subdirectory. Magento2 runs perfectly if not under a subdirectory.
Here is my settings, Wordpress is install under /var/www/wp, whereas Magento2 is intalled under /var/www/store, below is my configuration:
upstream fastcgi_backend {
server unix:/run/php/php7.0-fpm.sock;
}
server {
server_name www.example.com;
listen 80;
port_in_redirect off;
root /var/www/wp;
index index.html index.php;
error_page 419 = #magento;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /store {
root /var/www; #seems useless, it works the same even if commented.
index index.php;
autoindex off;
charset UTF-8;
#error_page 404 403 = /store/pub/errors/404.php; #if this line doesn't be commented, it will display a blank page.
location = /store {rewrite /store /store/index.php last;}
location = /store/ {rewrite /store /store/index.php last;}
location = /store/setup {rewrite /store/setup /store/setup/index.php last;}
location = /store/setup/ {rewrite /store/setup/ /store/setup/index.php last;}
location /store/setup {
location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
access_log off;
expires max;
try_files $uri =404;
}
location /store/setup/index.php {return 419;}
}
location /store/pub/ {
location /store/pub/media/ {
location /store/pub/media/customer/ {deny all;}
location /store/pub/media/downloadable/ {deny all;}
location ~ /store/pub/media/theme_customization/.*\.xml$ {deny all;}
try_files $uri $uri/ /store/pub/get.php?$args;
location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
add_header Cache-Control "public";
expires +1y;
try_files $uri $uri/ /store/pub/get.php?$args;
}
location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
add_header Cache-Control "no-store";
expires off;
try_files $uri $uri/ /store/pub/get.php?$args;
}
}
location /store/pub/static/ {
expires max;
location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
add_header Cache-Control "public";
expires +1y;
if (!-f $request_filename) {
rewrite ^/store/pub/static/(version\d*/)?(.*)$ /store/pub/static.php?resource=$2 last;
}
}
location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
add_header Cache-Control "no-store";
expires off;
if (!-f $request_filename) {
rewrite ^/store/pub/static/(version\d*/)?(.*)$ /store/pub/static.php?resource=$2 last;
}
}
if (!-f $request_filename) {
rewrite ^/store/pub/static/(version\d*/)?(.*)$ /store/pub/static.php?resource=$2 last;
}
}
location ~ ^/store/pub/errors/.*\.(xml|phtml)$ {deny all;}
location /store/pub/errors/ {try_files $uri =404;}
location = /store/pub/cron.php {deny all;}
}
location ~ (index|get|static|report|404|503|11)\.php$ { return 419; }
if (-e $request_filename) {return 403;}
rewrite /store /store/index.php last;
}
# Cache static files for as long as possible
location ~*.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf|cur)$ {
expires max;
log_not_found off;
access_log off;
}
# Deny public access to wp-config.php
location ~* wp-config.php {
deny all;
}
location ~ /\. {deny all;}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass fastcgi_backend;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location #magento {
root /var/www;
try_files $uri =404;
include fastcgi_params;
fastcgi_pass fastcgi_backend;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
www.example.com/store will return something as attached picture;
whereas typing http://www.example.com/store/setup resulted in 404ed and address bar became http://www.example.com/store/setup/index.php/session/unlogin.
Highly appreciated if anyone could give me a clue. Many thanks.
When I post to a 404 page, nginx is stripping the post data and changing the request method to "GET". How do I prevent this? I have nginx configured to serve a php file as the error 404 page that is in the root of the domain directory. I would like to keep a log of whatever is posted to it.
nginx.conf
server {
listen 80;
error_page 404 = /404.php;
set $oldhost $host;
root /var/www/www/$oldhost;
location ~* \.(?:ico|css|js|gif|jpg|png|html|htm)$ {
expires 3d;
add_header Pragma public;
add_header Cache-Control "public";
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
index index.php index.htm index.html;
try_files $uri $uri/ /index.php?$args;
}
location ~ .php$ {
location ~ \..*/.*\.php$ {return 404;}
expires 1d;
add_header Pragma public;
client_max_body_size 80m;
add_header Cache-Control "public";
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
This happens because when nginx cannot find a page, it will redirect (GET) the user to that 404.php page you defined.
What you need to do is to add it to the try_files directive, so nginx will try to post to it when it cannot find the others, or make it that you can handle 404 error directly on your index.php (like wordpress does).
EDIT:
location / {
index index.php index.htm index.html;
try_files $uri $uri/ /index.php?$args 404.php 404.php?$args;
}
Images are only displaying if they have the correct case. These need to be case insensitive please see my config file.
The image I have having trouble with works at:
http://domain.com/sites/default/files/vimages/imagename.jpg
but not at
http://domain.com/sites/default/files/vimages/imagename.JPG
server {
listen 80;
server_name domain.com;
root /home/domain.com/www; ## <-- Your only path reference.
access_log /home/domain.com/logs/access.log;
error_log /home/domain.com/logs/error.log;
# Enable compression, this will help if you have for instance advagg‎ module
# by serving Gzip versions of the files.
gzip_static on;
gzip on;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# This matters if you use drush
location = /backup {
deny all;
}
# Very rarely should these ever be accessed outside of your lan
location ~* \.(txt|log)$ {
allow 192.168.0.0/16;
deny all;
}
location ~ \..*/.*\.php$ {
return 403;
}
# No no for private
location ~ ^/sites/.*/private/ {
return 403;
}
# Block access to "hidden" files and directories whose names begin with a
# period. This includes directories used by version control systems such
# as Subversion or Git to store control files.
location ~ (^|/)\. {
return 403;
}
location / {
# This is cool because no php is touched for static content
try_files $uri #rewrite;
}
location #rewrite {
# Some modules enforce no slash (/) at the end of the URL
# Else this rewrite block wouldn't be needed (GlobalRedirect)
rewrite ^ /index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_read_timeout 180;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 52w;
add_header Cache-Control "max-age=31449600, no-transform, public";
log_not_found off;
}
# Catch image styles for D7 too.
location ~* ^/sites/.*/files/styles/ {
try_files $uri #rewrite;
}
location ~* /sites/.*/files/vimages/ {
try_files $uri #rewrite;
}
# Fighting with ImageCache? This little gem is amazing.
location ~ ^/sites/.*/files/imagecache/ {
try_files $uri #rewrite;
}
}
Nginx is working against the filesystem and has no control over the name of the files when serving from a directory.
If you want case insensitive you need to do a rewrite.