| Home | Mirror | Search |
# cat /etc/nginx/conf.d/images.conf
server {
listen 80;
server_name images.example.com;
#charset koi8-r;
access_log /var/log/nginx/images.access.log main;
location / {
root /www/images;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
绑定多个域名
server_name images.example.com img1.example.com img2.example.com;
使用通配符匹配
server_name *.example.com server_name www.*;
正则匹配
server_name ~^(.+)\.example\.com$; server_name ~^(www\.)?(.+)$;
location / {
root /www;
index index.html index.htm;
}
location ~ ^/(config|include)/ {
deny all;
break;
}
mkdir /etc/nginx/ssl
cp your_ssl_certificate to /etc/nginx/ssl
# HTTPS server
#
server {
listen 443;
server_name localhost;
root html;
index index.html index.htm;
ssl on;
#ssl_certificate cert.pem;
ssl_certificate ssl/example.com.pem;
ssl_certificate_key ssl/example.com.key;
ssl_session_timeout 5m;
ssl_protocols SSLv3 TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
ssl_prefer_server_ciphers on;
location / {
try_files $uri $uri/ /index.html;
}
}
configtest
$ sudo service nginx configtest Testing nginx configuration: nginx.
443 port test
$ openssl s_client -connect www.example.com:443
expires 格式
例 13.1. Expires Examples
expires 1 January, 1970, 00:00:01 GMT; expires 60s; expires 30m; expires 24h; expires 1d; expires max; expires off; expires 24h; expires modified +24h; expires @15h30m; expires 0; expires -1; expires epoch; add_header Cache-Control private;
注意:expires仅仅适用于200, 204, 301, 302,304
单个文件匹配
location ~* \.css$ {
expires 30d;
}
扩展名匹配
#图片类资源缓存5天,并且不记录请求日志
location ~ .*\.(ico|gif|jpg|jpeg|png|bmp|swf)$
{
expires 5d;
access_log off;
}
#css/js 缓存一天,不记录请求日志
location ~ .*\.(js|css)$
{
expires 1d;
access_log off;
}
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{
expires 30d;
}
location ~ .*\.(js|css)$
{
expires 1h;
}
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
expires 1h;
break;
}
}
location ~* \.(jpg|jpeg|gif|css|png|js|ico)$ {
expires max;
}
#cache control: all statics are cacheable for 24 hours
location / {
if ($request_uri ~* \.(ico|css|js|gif|jpe?g|png)$) {
expires 72h;
break;
}
}
add_header 实例
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
例 13.2. nginx expires
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ {
expires 1d;
access_log off;
}
location ~ .*\.(js|css)$ {
expires 1d;
access_log off;
}
location ~ .*\.(html|htm)$
{
expires 1d;
access_log off;
}
#防止access文件被下载
location ~ /\.ht {
deny all;
}
location ~ ^/upload/.*\.php$
{
deny all;
}
location ~ ^/static/images/.*\.php$
{
deny all;
}
location ~ /\.ht {
deny all;
}
location ~ .*\.(sqlite|sq3)$ {
deny all;
}
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 "Login";
auth_basic_user_file htpasswd;
}
}
生成密码文件
$ sudo apt-get install apache2-utils htpasswd -c -d htpasswd user_name
必须使用 -d Force CRYPT encryption of the password. 选项,
# vim /etc/nginx/sites-enabled/default
location / {
autoindex on;
}
# /etc/init.d/nginx reload Reloading nginx configuration: nginx.
http {
ssi on;
}
location / {
ssi on;
ssi_silent_errors on;
ssi_types text/shtml;
}
ssi on; ssi_silent_errors on; ssi_types text/shtml; ssi_value_length 256; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 8m;
Rewrite Flags last - 基本上都用这个Flag。 break - 中止Rewirte,不在继续匹配 redirect - 返回临时重定向的HTTP状态302 permanent - 返回永久重定向的HTTP状态301 文件及目录匹配,其中: -f和!-f用来判断是否存在文件 -d和!-d用来判断是否存在目录 -e和!-e用来判断是否存在文件或目录 -x和!-x用来判断文件是否可执行 正则表达式全部符号解释 ~ 为区分大小写匹配 ~* 为不区分大小写匹配 !~和!~* 分别为区分大小写不匹配及不区分大小写不匹配 (pattern) 匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0…$9 属性。要匹配圆括号字符,请使用 ‘\(’ 或 ‘\)’。 ^ 匹配输入字符串的开始位置。 $ 匹配输入字符串的结束位置。
server {
listen 80;
server_name www.example.com example.com ;
if ($host = "example.com" )
{
rewrite ^/(.*)$ http://www.example.com/$1 permanent;
}
if ($host != "www.example.com" )
{
rewrite ^/(.*)$ http://www.example.com/$1 permanent;
}
}
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (!-f $request_filename){
rewrite /(.*) http://images.example.com/$1;
}
}
if ($host ~ '(.*)\.static\.example\.com' ) {
set $subdomain $1;
rewrite "^/(.*)$" /$subdomain/$1;
}
gzip on; gzip_min_length 1000; gzip_buffers 4 8k; gzip_types text/plain application/x-javascript text/css text/html application/xml; gzip on; gzip_http_version 1.0; gzip_disable "MSIE [1-6]."; gzip_types text/plain application/x-javascript text/css text/javascript;
add_header Nginx-Cache "HIT from www.example.com"; or add_header Nginx-Cache "$upstream_cache_status from www.example.com";
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
# 相关页面设置Cache-Control头信息
if ($request_uri ~* "^/$|^/news/.+/|^/info/.+/") {
add_header Cache-Control max-age=3600;
}
if ($request_uri ~* "^/suggest/|^/categories/") {
add_header Cache-Control max-age=86400;
}
location ~* \.(eot|ttf|woff)$ {
add_header Access-Control-Allow-Origin *;
}
location /js/ {
add_header Access-Control-Allow-Origin https://www.mydomain.com/;
add_header Access-Control-Allow-Methods GET,OPTIONS;
add_header Access-Control-Allow-Headers *;
}
location / {
if ($request_method = OPTIONS ) {
add_header Access-Control-Allow-Origin "http://example.com";
add_header Access-Control-Allow-Methods "GET, OPTIONS";
add_header Access-Control-Allow-Headers "Authorization";
add_header Access-Control-Allow-Credentials "true";
add_header Content-Length 0;
add_header Content-Type text/plain;
return 200;
}
}
例 13.3. 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;
}
}