2016-01-08 闲来无事,整理下思绪
http { #嵌入配置文件的根部, 一个http里可以配置多个server
server { #声明一个站点
server_name www.website.com; #监听的主机名
listen 80; #监听套接字所使用的ip地址和端口号
error_page 404 /not_found.html;
error_page 500 501 502 503 504 /server_error.html;
index index.html;
root /var/www/website/com/html; #定义文档的根目录
#location, 通过制定的模式与客户端请求的URI相匹配
location / { #网站的特定位置
}
location /admin/ { #网站的特定位置 #
alias /var/www/locked/; #只能放在 location区段中,为指定路径提供别名
}
#操作符,匹配时跟定义顺序无关
location = /abcd { #精确匹配,不能用正则
}
location /abc/ { #url必须以指定模式开始,不能用正则
}
location ^~ /abcd$ { #吴标致行为,URI定位必须以指定模式开始,如果匹配,停止搜索其他模式
}
location ~ ^/abcd$ { #正则匹配,区分大小写
}
location ~* ^/abcd$ { #正则匹配,不区分大小写
}
location @test { #定义location区段名,客户端不能访问,内部产生的请求可以,例如try_files或error_page
}
}
}加到http模块中, 开启gzip, 注意gzip_types配置得是压缩的资源类型
nginx.conf
http {
gzip on;
gzip_min_length 1k;
gzip_comp_level 5;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/javascript text/javascript application/x-javascript text/xml application/xml application/xml+rss application/json image/x-icon image/png image/jpg image/jpeg application/font-woff;
gzip_vary on;
}nginx.conf
worker_processes 4;
events {
worker_connections 2048;
use epoll;
multi_accept on;
}
worker_rlimit_nofile 100000; location ~* \.(?:css|js)$ {
expires 12h;
access_log off;
add_header Cache-Control "public";
proxy_pass http://127.0.0.1:5000;
proxy_redirect off;
} location /
{
proxy_pass http://127.0.0.1:8000;
proxy_pass_header Server;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
}可以设置超时时间
proxy_connect_timeout 500s; proxy_read_timeout 500s; proxy_send_timeout 500s;
location /movies/ {
alias /Volumes/Media/Movies/;
allow all;
}
location = /miles.txt {
alias /data/www/static/miles.txt;
expires 30d;
access_log off;
}server {
listen 80;
server_name www.cloudray.cn;
client_max_body_size 1M;
access_log logs/blog_access.log;
error_log logs/blog_error.log;
root /data/static_site_dir;
index index.html;}return http_code;
return http_code "content";
location /api/test/ {
return 403;
}
location /stat/ {
return 204;
}
location /ping/ {
return 200;server {
server_name cloudray.com;
rewrite ^(.*) http://www.abc.com$1 permanent;if (-f $uri) {
break
}
if ($uri ~ ^/admin/){
return 403;
}
if ($uri ~ ^/search/(.*)$) {
set $query $1;
rewrite ^ /search.php?q=$query?;
}
rewrite A B option;
options:
last :表示完成rewrite
break:本规则匹配完成后,终止匹配,不再匹配后面的规则
redirect:返回302临时重定向,地址栏会显示跳转后的地址
permanent:返回301永久重定向,地址栏会显示跳转后的地址location /test/ {
allow 192.168.1.1;
deny all;
}http {
upstream up_name {
server 192.168.0.1:9000 weight=5; #权重
server 192.168.0.2:9000 weight=5 max_fails=5 fail_timeout=60s; #在60s内,其错误通信超过5次,认为该服务失效
server 192.168.0.3:9000 down; #服务标记为离线,不再使用
server 192.168.0.4:9000 backup; #备份服务器,其他全部宕机了才启用
}
server {
location / {
proxy_pass A;
}
}location ~ \.(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt)$ {
root /opt/webapp;
expires 24h;
}
expires 1 January, 1970, 00:00:01 GMT;
expires 60s;
expires 30m;
expires 24h;
expires 1d;
expires max;
expires off;$arg_PARAMETER 这个变量包含在查询字符串时GET请求PARAMETER的值。 $args 这个变量等于请求行中的参数。 $binary_remote_addr 二进制码形式的客户端地址。 $body_bytes_sent $content_length 请求头中的Content-length字段。 $content_type 请求头中的Content-Type字段。 $cookie_COOKIE cookie COOKIE的值。 $document_root 当前请求在root指令中指定的值。 $document_uri 与$uri相同。 $host 请求中的主机头字段,如果请求中的主机头不可用,则为服务器处理请求的服务器名称。 $is_args 如果$args设置,值为"?",否则为""。 $limit_rate 这个变量可以限制连接速率。 $nginx_version 当前运行的nginx版本号。 $query_string 与$args相同。 $remote_addr 客户端的IP地址。 $remote_port 客户端的端口。 $remote_user 已经经过Auth Basic Module验证的用户名。 $request_filename 当前连接请求的文件路径,由root或alias指令与URI请求生成。 $request_body 这个变量(0.7.58+)包含请求的主要信息。在使用proxy_pass或fastcgi_pass 指令的location中比较有意义。 $request_body_file 客户端请求主体信息的临时文件名。 $request_completion 请求完成 $request_method 这个变量是客户端请求的动作,通常为GET或POST。包括0.8.20及之前的版本中, 这个变量总为main request中的动作,如果当前请求是一个子请求,并不使用这个当前请求的动作。 $request_uri 这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或 重写URI。 $schemeHTTP 方法(如http,https)。按需使用,例: rewrite ^(.+)$ $scheme://example.com$1 redirect; $server_addr 服务器地址,在完成一次系统调用后可以确定这个值,如果要绕开系统调用, 则必须在listen中指定地址并且使用bind参数。 $server_name 服务器名称。 $server_port 请求到达服务器的端口号。 $server_protocol 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。 $uri 请求中的当前URI(不带请求参数,参数位于$args),可以不同于浏览器传递的$request_uri的值 ,它可以通过内部重定向,或者使用index指令进行修改。
本文出自 “Miles Away” 博客,请务必保留此出处http://milesaway.blog.51cto.com/4984800/1732828
原文:http://milesaway.blog.51cto.com/4984800/1732828