Вернуться назад

Пример конфига Nginx с кешем

Пример конфига nginx с кешированием, для игнорирования кеша на определенных разделах.

 

####################### site.ru front-end ###############################################################################
server
{
listen 10.10.10.10:80;
server_name site.ru www.site.ru;
root /var/www/site.ru;

include /usr/local/include/block.conf;
#include /usr/local/include/def.conf;

gzip on;
gzip_comp_level 9;
gzip_disable "msie6";
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/bmp image/png image/gif image/jpeg image/jpg;

access_log /var/log/nginx/site-front.cache cache;
access_log /var/log/nginx/site-front.access goaccess;
error_log /var/log/nginx/site-front.error;

set $var_desktop "desktop";
set $var_mobile "phones";
set $do_not_cache 0;
if ($request_uri ~* "report.html|/contact-us/|/wp-admin/|/xmlrpc.php|/feed/|sitemap(_index)?.xml|.*SuperSocializer.*|/watchlist/") {
set $do_not_cache 1;
}
if ($http_cookie ~* "report.html|comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $do_not_cache 1;
}
rewrite "/video/(.*)" /video/movie.mp4 break;
rewrite "/(picture|photo|photograph|placard|snapshot|poster|cover|artwork|gallery|media)\.php(.*)" /image.php$2 last;
location / {
proxy_no_cache $do_not_cache;
proxy_cache_bypass $do_not_cache;
proxy_pass http://127.0.0.1:8080/;
proxy_cache pagecache;
proxy_cache_key "$host$request_uri$groupname";
# proxy_cache_key "$scheme$host$uri$is_args$args$groupname";
proxy_redirect off;
proxy_cache_valid 24h;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass_header Set-Cookie;
}
location ~ /purge(\/.*) {
allow 127.0.0.1 ;
allow 207.244.108.238;
deny all;
proxy_cache_purge pagecache "$host$1$var_desktop";
}
location ~ /mpurge(\/.*) {
allow 127.0.0.1 ;
allow 10.10.10.10;
deny all;
proxy_cache_purge pagecache "$host$1$var_mobile";
}
location ~ /\. {
deny all;
}
}

#################### backend #############################################################################
server
{
listen 127.0.0.1:8080;
server_name site.ru www.site.ru;
root /var/www/site.ru;
index index.php index.html index.htm;

access_log /var/log/nginx/site-back.access;
error_log /var/log/nginx/site-back.error;
access_log /var/log/nginx/site-back.cache cache;

set $granted "$allowed_country:$allowed_ip";
if ($granted = no:no) {
return 404;
}

location / {
try_files $uri $uri/ /index.php?$args;
}
# location ~* /(wp-login.php) {
# auth_basic "closed site";
# auth_basic_user_file /var/www/htpasswd;
# }
location /wp-admin {
satisfy any;
auth_basic "closed site";
auth_basic_user_file /var/www/htpasswd;
}

location ~*/wp-login.php {
auth_basic "closed site";
auth_basic_user_file /var/www/htpasswd;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

location /phpmyadmin {
auth_basic "closed site";
auth_basic_user_file /var/www/htpasswd;
}

set $skip_cache 0;
if ($request_method = POST) {
set $skip_cache 1;
}
# if ($query_string != "") {
# set $skip_cache 1;
# }
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml|/shows|/genre|/network") {
set $skip_cache 1;
}
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in|wp-postpass") {
set $skip_cache 1;
}
rewrite "/(picture|photo|photograph|placard|snapshot|poster|cover|artwork|gallery|media)\.php(.*)" /image.php$2 last;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# fastcgi_cache FCGI_CACHE;
# fastcgi_cache_key "$scheme$request_method$host$request_uri$groupname";
# fastcgi_cache_valid any 10m;
# fastcgi_cache_use_stale updating error timeout invalid_header http_500;
# fastcgi_cache_bypass $skip_cache;
# fastcgi_no_cache $skip_cache;
# fastcgi_hide_header "Cache-Control";
# fastcgi_hide_header "Expires";
# fastcgi_ignore_headers "Cache-Control" "Expires";
fastcgi_param REMOTE_ADDR $http_x_real_ip;
fastcgi_pass unix:/var/run/php5-fpm.sock;

fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
#fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include /etc/nginx/fastcgi_params;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
valid_referers blocked server_names;
if ($invalid_referer) {
rewrite ^ https://site.ru redirect;
}
expires max;
log_not_found off;
}
}


igel
Дата: 25 января 2019 г. в 01:19:26

Автор: igel