| 知乎專欄 | 多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者” |
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
php-fpm 狀態
location ~ ^/(status|ping)$ {
access_log off;
allow 202.82.21.12;
deny all;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
include fastcgi_params;
}
location / {
sub_filter '<a href="http://127.0.0.1:8080/' '<a href="https://$host/';
sub_filter '<img src="http://127.0.0.1:8080/' '<img src="https://$host/';
sub_filter_once on;
}
替換掉proxy_pass頁面中的內容
location ~ ^/live800 {
proxy_pass http://218.23.24.53;
rewrite ^/live800/(.*) /$1 break;
proxy_set_header Accept-Encoding "";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host www.abc.com;
sub_filter_types text/html text/css text/xml text/css text/javascript;
sub_filter 'www.abc.com' '$host';
sub_filter_once off;
}
cd /usr/local/nginx/conf
server {
listen 80;
server_name www.example.com;
root /var/www/htdocs;
index index.html;
location / {
try_files $uri $uri/ /index.html;
auth_basic "Administrator’s Area";
auth_basic_user_file conf/htpasswd;
}
}
生成密碼檔案
$ sudo apt-get install apache2-utils htpasswd -c -d htpasswd user_name
![]() | 提示 |
|---|---|
|
必須使用 -d Force CRYPT encryption of the password. 選項, |
例 45.4. Example: valid_referers
location /photos/ {
valid_referers none blocked www.mydomain.com mydomain.com;
if ($invalid_referer) {
return 403;
}
}
location ~* \.(gif|jpg|jpeg|png|bmp|txt|zip|jar|swf)$ {
valid_referers none blocked *.mydomain.com;
if ($invalid_referer) {
rewrite ^/ http://www.mydomain.com/default.gif;
#return 403;
}
}
location /images/ {
alias /www/images/;
valid_referers none blocked *.mydomain.com;
if ($invalid_referer) {
rewrite ^/ http://www.mydomain.com/default.gif;
}
}
location /video/ {
mp4;
mp4_buffer_size 1m;
mp4_max_buffer_size 5m;
mp4_limit_rate on;
mp4_limit_rate_after 30s;
}
limit_zone one $binary_remote_addr 10m;
server {
location /download/ {
limit_conn one 1;
}
image_filter 配置項: image_filter off; 在所在location關閉模組處理。 image_filter test; 確保應答是JPEG,GIF或PNG格式的圖像。否則錯誤 415 (Unsupported Media Type) 將被返回。 image_filter size; 以JSON格式返回圖像信息。 image_filter rotate 90 | 180 | 270; 將圖像逆時針旋轉指定角度。 參數的值可以包含變數。 可以單獨使用,或與 resize 和 crop 變換同時使用. image_filter resize width height; 按比例縮小圖像至指定大小。 如果想只指定其中一維,另一維可以指定為: “-”。 如果有錯誤發生,伺服器會返回 415 (Unsupported Media Type). 參數的值可以包含變數。 當與 rotate 參數同時使用時, 旋轉發生在縮放 之後。 image_filter crop width height; 按比例以圖像的最短邊為準對圖像大小進行縮小,然後裁剪另一邊多出來的部分。 如果想只指定其中一維,另一維可以指定為: “-”。 如果有錯誤發生,伺服器會返回 415 (Unsupported Media Type). 參數的值可以包含變數。 當與 rotate 參數同時使用時, 旋轉發生在裁剪 之前。 image_filter_buffer 配置項: image_filter_buffer size; 例如 image_filter_buffer 1M; 設置用來讀圖像的緩衝區的最大值。 若圖像超過這個大小,伺服器會返回 415 (Unsupported Media Type). image_filter_jpeg_quality quality; 例如 image_filter_jpeg_quality 75;設置變換後的JPEG圖像的 質量 。 可配置值: 1 ~ 100 。 更小的值意味着更差的圖像質量以及更少需要傳輸的數據。 推薦的最大值是95. 參數的值可以包含變數。 image_filter_sharpen percent; image_filter_sharpen 0; 增加最終圖像的鋭度。 鋭度百分比可以超過100. 0為關閉鋭化。 參數的值可以包含變數。 image_filter_transparency on|off; image_filter_transparency on;定義當對PNG,或者GIF圖像進行顏色變換時是否需要保留透明度。 損失透明度有可能可以獲得更高的圖像質量。 PNG圖像中的alpha通道的透明度預設會一直被保留。
比如所有的圖片並修改尺寸為 800x600
location ~* \.(jpg|gif|png)$ {
image_filter resize 800 600;
}
匹配images目錄所有圖片並修改尺寸為1920x1080
location ~* /images/.*\.(jpg|gif|png)$ {
image_filter resize 1920 1080;
}
再比如用url來指定
location ~* (.*\.(jpg|gif|png))!(.*)x(.*)$ {
set $width $3;
set $height $4;
rewrite "(.*\.(jpg|gif|png))(.*)$" $1;
}
location ~* .*\.(jpg|gif|png)$ {
image_filter resize $width $height;
}
location ~* /images/(.+)_(d+)x(d+).(jpg|gif|png)$ {
set $height $2;
set $width $3;
if ($height = "0") {
rewrite /images/(.+)_(d+)x(d+).(jpg|gif|png)$ /images/$1.$4 last;
}
if ($width = "0") {
rewrite /images/(.+)_(d+)x(d+).(jpg|gif|png)$ /images/$1.$4 last;
}
#根據給定的長寬生成縮略圖
image_filter resize $height $width;
#原圖最大2M,要裁剪的圖片超過2M返回415錯誤,根據你的需求調節參數image_filter_buffer
image_filter_buffer 2M;
#error_page 415 /images/404.jpg;
try_files /images/$1.$4 /images/404.jpg;
}
location ~* /images {
}
location ~* ^/images/resize/([\d\-]+)_([\d\-]+)/(.+) {
alias /www/example.com/img.example.com/$3;
image_filter test;
image_filter resize $1 $2;
image_filter_buffer 2M;
image_filter_jpeg_quality 95;
image_filter_sharpen 90;
expires 60d;
}
ngx_stream_proxy_module 用法與 ngx_http_proxy_module 及其相似, 前者用於tcp代理或負載均衡。後者只能用於 http 的代理
注意模組的proxy_pass指令只能在server段使用, 提供域名或ip地址和連接埠轉發,協議可以是tcp,也可以是udp。
server {
listen 127.0.0.1:80;
proxy_pass 127.0.0.1:8080;
}
server {
listen 25;
proxy_connect_timeout 1s;
proxy_timeout 1m;
proxy_pass mail.example.com:25;
}
server {
listen 53 udp;
proxy_responses 1;
proxy_timeout 20s;
proxy_pass dns.example.com:53;
}
server {
listen [::1]:8000;
proxy_pass unix:/tmp/stream.socket;
}
location / {
mirror /mirror;
proxy_pass http://backend;
}
location /mirror {
internal;
proxy_pass http://test_backend$request_uri;
}
location /api/ {
limit_except PUT DELETE {
proxy_pass http://127.0.0.1:9080;
}
}