Nginx
Last Edited Time
Feb 9, 2022 06:51 AM
date
Jan 3, 2018
slug
nginx
status
Published
tags
Nginx
Notebook
summary
Nginx 常用配置
type
Post
Nginx 日志系统搭建
filebeat – > kafka –> logstash –> elasticsearch –> Grafana 还有一个输出是到 kibana 的这个可以用来分析问题
zabix 主要是监控服务器,凡懿把 nginx log 搞到 es 里了,再用 grafana 可视化
Rewrite
Regex except word ‘index’
^\/(?!index$)[\w-]+$
nginx remove .php from url
location / {
try_files $uri $uri/ $uri.php?$args;
}
if ($request_uri ~ ^/([^?]*)\.php($|\?)) {
return 302 /$1;
}
web/variables.conf
# determain if current client is desktop website
map $http_user_agent $is_desk {
default 0;
~*linux.*android|windows\s+(?:ce|phone) 0; # exceptions to the rule
~*spider|crawl|slurp|bot 1; # bots
~*windows|linux|os\s+x\s*[\d\._]+|solaris|bsd|safarinotificationagent 1; # OSes
}
## Revert the logic.
map $is_desk $is_mobile {
1 0;
0 1;
}
map $host $is_independent {
default 1;
~*m.goopter.com|m.goopter.cn|shop.goopter.com 0;
}
erb/m.conf
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
set $desktop_independent_flag "${is_desk}${is_independent}";
# redirect if it is desktop website
if ($desktop_independent_flag = '10') {
return 301 https://shop.goopter.com$uri?$args;
}
if ($desktop_independent_flag = '11') {
return 301 https://shop.goopter.com/redirect/?page=store&domain=$host&from=mobile;
}
location / {
try_files $uri $uri.html $uri/ $uri.php?$args;
}
# for independent domain / rewrite
location = / {
if ($is_independent) {
rewrite (.*) /store/;
}
try_files $uri $uri.html $uri/ $uri.php?$args;
}
# for independent domain /$product_url_key rewrite
location ~* ^\/(?!index$)[\w-]+$ {
if ($is_independent) {
rewrite ^\/([\w-]+) /store/product/?pid=$1;
}
try_files $uri $uri.html $uri/ $uri.php?$args;
}
# for /store/$store_url_key
# because the independent domain root url will redirect to /index.php,
# we need to disable rewrite for rewrite situation
location ~* ^\/store\/(?!index$)[\w-]+$ {
rewrite ^\/store\/([\w-]+) /store/?gid=$1;
}
# for /store/$store_url_key/$product_url_key
location ~* ^\/store\/product\/([\w-]+)\/([\w-]+)$ {
rewrite ^\/store\/product\/([\w-]+)\/([\w-]+)$ /store/product/?gid=$1&pid=$2;
}
# disable /$store_category access for independ domain
# location ~* ^\/(restaurant|service|shopping|travel|groupsale)(\/)?.*$ {
# if ($is_independent) {
# rewrite (.*) /;
# }
# try_files $uri $uri.html $uri/ $uri.php?$args;
# }
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_intercept_errors off;
include /etc/nginx/fastcgi_params;
}
location ~* ^.+\.(js|css)$ {
expires -1;
}
Chrome SameSite=Lax 策略更新导致的问题
# proxy_params
# 以下解决方法在安卓微信浏览器内核中不能设置 cookie, 顾在渲染引擎项目中禁止
# fix Chrome cookie SameSite=Lax bug
# Reference: https://www.chromium.org/updates/same-site/test-debug
proxy_cookie_path / "/; Secure; SameSite=None";
Nginx 转发丢失 $scheme 问题(missing https protocol)
set $mscheme $scheme;
if ($http_referer ~* ^https.*) {
set $mscheme "https";
}
proxy_set_header X-Forwarded-Proto $mscheme;