| 知乎专栏 |
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. 选项, |
例 35.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;
}
}