本节内容:
nginx转发规则
nginx正则匹配
正则表达式匹配,其中:
* ~* 为不区分大小写匹配
* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配
文件及目录匹配,其中:
* -f和!-f用来判断是否存在文件
* -d和!-d用来判断是否存在目录
* -e和!-e用来判断是否存在文件或目录
* -x和!-x用来判断文件是否可执行
flag标记有:
* break 终止匹配, 不再匹配后面的规则
* redirect 返回302临时重定向 地址栏会显示跳转后的地址
* permanent 返回301永久重定向 地址栏会显示跳转后的地址
一些可用的全局变量有,可以用做条件判断(待补全)
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri
结合QeePHP的例子
rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$ /index.php?namespace=user&controller=$1&action=$2&$3 last;
rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&controller=$1 last;
break;
多目录转成参数
if ($host ~* (.*)/.domain/.com) {
set $sub_name $1;
rewrite ^/sort//(/d+)//?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
}
目录对换
rewrite ^/(/d+)/(.+)/ /$2?id=$1 last;
例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下:
rewrite ^(.*)$ /nginx-ie/$1 break;
}
目录自动加“/”
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
禁止htaccess
deny all;
}
禁止多个目录
deny all;
break;
}
禁止以/data开头的文件
可以禁止/data/下多级目录下.log.txt等请求;
deny all;
}
禁止单个目录
不能禁止.log.txt能请求
deny all;
}
禁止单个文件
deny all;
}
给favicon.ico和robots.txt设置过期时间;
这里为favicon.ico为99天,robots.txt为7天并不记录404错误日志
log_not_found off;
expires 99d;
break;
}
location ~(robots.txt) {
log_not_found off;
expires 7d;
break;
}
设定某个文件的过期时间;这里为600秒,并不记录访问日志
access_log off;
root /opt/lampp/htdocs/web;
expires 600;
break;
}
文件反盗链并设置过期时间
这里的return 412 为自定义的http状态码,默认为403,方便找出正确的盗链的请求
“rewrite ^/ http://www./leech.gif;”显示一张防盗链图片
“access_log off;”不记录访问日志,减轻压力
“expires 3d”所有文件3天的浏览器缓存
valid_referers none blocked *. *.xxx.net localhost 208.97.167.194;
if ($invalid_referer) {
rewrite ^/ http://www./leech.gif;
return 412;
break;
}
access_log off;
root /opt/lampp/htdocs/web;
expires 3d;
break;
}
只充许固定ip访问网站,并加上密码
allow 208.97.167.194;
allow 222.33.1.2;
allow 231.152.49.4;
deny all;
auth_basic "C1G_ADMIN";
auth_basic_user_file htpasswd;
将多级目录下的文件转成一个文件,增强seo效果
/job-123-456-789.html 指向/job/123/456/789.html
rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)/.html$ /job/$1/$2/jobshow_$3.html last;
将根目录下某个文件夹指向2级目录
如/shanghaijob/ 指向 /area/shanghai/
如果你将last改成permanent,那么浏览器地址栏显是/location/shanghai/
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
上面例子有个问题是访问/shanghai 时将不会匹配
rewrite ^/([0-9a-z]+)job$ /area/$1/ last;
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
这样/shanghai 也可以访问了,但页面中的相对链接无法使用,
如./list_1.html真实地址是/area/shanghia/list_1.html会变成/list_1.html,导至无法访问。
那我加上自动跳转也是不行咯
(-d $request_filename)它有个条件是必需为真实目录,而我的rewrite不是的,所以没有效果
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
知道原因后就好办了,让我手动跳转吧
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
文件和目录不存在的时候重定向:
proxy_pass http://127.0.0.1;
}
域名跳转
{
listen 80;
server_name jump.;
index index.html index.htm index.php;
root /opt/lampp/htdocs/www;
rewrite ^/ http://www./;
access_log off;
}
多域名转向
index index.html index.htm index.php;
root /opt/lampp/htdocs;
if ($host ~ "xxx/.net") {
rewrite ^(.*) http://www.$1 permanent;
}
三级域名跳转
rewrite ^(.*) http://top.yingjiesheng.com$1;
break;
}
域名镜向
{
listen 80;
server_name mirror.;
index index.html index.htm index.php;
root /opt/lampp/htdocs/www;
rewrite ^/(.*) http://www./$1 last;
access_log off;
}
某个子目录作镜向
rewrite ^.+ http://zph./ last;
break;
}
discuz ucenter home (uchome) rewrite
rewrite ^/(space|network)-(.+)/.html$ /$1.php?rewrite=$2 last;
rewrite ^/(space|network)/.html$ /$1.php last;
rewrite ^/([0-9]+)$ /space.php?uid=$1 last;
discuz 7 rewrite
rewrite ^(.*)/archiver/((fid|tid)-[/w/-]+/.html)$ $1/archiver/index.php?$2 last;
rewrite ^(.*)/forum-([0-9]+)-([0-9]+)/.html$ $1/forumdisplay.php?fid=$2&page=$3 last;
rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)/.html$ $1/viewthread.php?tid=$2&extra=page/%3D$4&page=$3 last;
rewrite ^(.*)/profile-(username|uid)-(.+)/.html$ $1/viewpro.php?$2=$3 last;
rewrite ^(.*)/space-(username|uid)-(.+)/.html$ $1/space.php?$2=$3 last;
rewrite ^(.*)/tag-(.+)/.html$ $1/tag.php?name=$2 last;
给discuz某版块单独配置域名
location = / {
if ($http_host ~ news/.$) {
rewrite ^.+ http://news./forum-831-1.html last;
break;
}
}
discuz ucenter 头像 rewrite 优化
location ~ .*/.php?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location /ucenter/data/avatar {
log_not_found off;
access_log off;
location ~ /(.*)_big/.jpg$ {
error_page 404 /ucenter/images/noavatar_big.gif;
}
location ~ /(.*)_middle/.jpg$ {
error_page 404 /ucenter/images/noavatar_middle.gif;
}
location ~ /(.*)_small/.jpg$ {
error_page 404 /ucenter/images/noavatar_small.gif;
}
expires 300;
break;
}
}
jspace rewrite
location ~ .*/.php?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~* ^/index.php/
{
rewrite ^/index.php/(.*) /index.php?$1 break;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
本节内容:
nginx图片过滤处理模块http_image_filter_module
http_image_filter_module是nginx提供的集成图片处理模块,支持nginx-0.7.54以后的版本,在网站访问量不是很高磁盘有限不想生成多余的图片文件的前提下可,就可以用它实时缩放图片,旋转图片,验证图片有效性以及获取图片宽高以及图片类型信息,由于是即时计算的结果,所以网站访问量大的话,不建议使用。
安装过程很简单,默认HttpImageFilterModule模块是不会编译进nginx的,所以要在configure时指定:
注意:
HttpImageFilterModule模块需要依赖gd-devel的支持,可以使用yum或apt-get方便地安装。
如果未安装回报“/configure: error: the HTTP image filter module requires the GD library.”错误
apt-get install libgd2-xpm libgd2-xpm-dev
make&&make install
#proxy_pass http://10.11.11.11; #rewrite "/simg/(.*\.jpg)$" /img/$1 break ;
image_filter resize 100 100;
error_page 415 = /empty;
}
当然,也可以开启重写去读取本机另一个目录下源文件;如果不在一台机器上就可以开启proxy_pass,并加上重写即可。
http_image_filter_module支持5种指令:
image_filter:测试图片文件合法性(image_filter test);3个角度旋转图片(image_filter rotate 90 | 180 | 270);以json格式输出图片宽度、高度、类型(image_filter size);最小边缩小图片保持图片完整性(resize width height);以及最大边缩放图片后截取多余的部分(image_filter crop [width] [height]);
image_filter_jpeg_quality:设置jpeg图片的压缩质量比例(官方最高建议设置到95,但平时75就可以了);
image_filter_buffer:限制图片最大读取大小,默认为1M;
image_filter_transparency:用来禁用gif和palette-based的png图片的透明度,以此来提高图片质量。
image_filter_sharpen:这个指令在nginx-1.1.8和1.0.11版本后增加的
示例:
#if ($request_filename ~ ([0-9]+)-([0-9]+)\.(png|jpe?g|gif|ico)) {
set $img_width $1;
set $img_height $2;
rewrite ^(.*)-[0-9]+-[0-9]+\.(png|jpe?g|gif|ico)$ /$1.$2 break;
# }
image_filter resize $img_width $img_height;
image_filter_buffer 2M;
expires 1y;
access_log off;
gzip off;
}
本节内容:
Nginx虚拟主机配置
说明:配置之前先把域名解析到服务器IP地址上
站点1:bbs. 程序所在目录/data/xxx/bbs
站点2:sns. 程序所在目录/data/xxx/sns
chown www.www /data/xxx/ -R #设置目录所有者,www为nginx运行账户
chmod 700 /data/xxx/ -R #设置目录权限
nginx配置文件路径:/usr/local/nginx/conf/nginx.conf
修改之前先备份原来的配置文件cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.confbak
一、禁止nginx空主机头
vi /usr/local/nginx/conf/nginx.conf #编辑
找到server,在上面一行添加如下内容:
listen 80 default;
server_name _;
location / {
root html;
return 404;
}
location ~ /.ht {
deny all;
}
}
设置之后,空主机头访问会直接跳转到nginx404错误页面。
二、添加nginx虚拟主机包含文件
cd /usr/local/nginx/conf/ #进入nginx安装目录
mkdir vhost #建立虚拟目录
vi /usr/local/nginx/conf/nginx.conf #编辑
找到上一步添加的代码,在最后添加如下内容:
include vhost/*.conf;
例如:
listen 80 default;
server_name _;
location / {
root html;
return 404;
}
location ~ /.ht {
deny all;
}
}
include vhost/*.conf;
三、修改nginx连接fastcgi的方式为unix domain socket
touch /tmp/php-cgi.sock #建立php-cgi.sock文件
chown www.www /tmp/php-cgi.sock #设置文件所有者为www(必须与nginx的用户一致)
vi /usr/local/nginx/conf/nginx.conf #编辑nginx配置文件
fastcgi_pass unix:/tmp/php-cgi.sock;//如果使用的是php-fpm需要修改/etc/php/fpm/php-fpm.conf listen /tmp/php-cgi.sock;
vi /usr/local/php5/etc/php-fpm.conf #编辑php-fpm配置文件
listen = 127.0.0.1:9000 修改为
listen =/tmp/php-cgi.sock;
四、修改好之后的/usr/local/nginx/conf/nginx.conf配置文件如下(建议直接使用这个修改好的文件)
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#keepalive_timeout 0;
keepalive_timeout 60;
tcp_nodelay on;
server_tokens off;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server {
listen 80 default;
server_name _;
location / {
root html;
return 404;
}
location ~ /.ht {
deny all;
}
}
server
{
listen 80;
#server_name localhost;
index index.php default.php index.html index.htm default.html default.htm ;
root /data/xxx;
location ~ .*\.(php|php5)?$
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
location /status {
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
access_log off;
}
include vhost/*.conf;
}
五、创建fcgi.conf配置文件
vi /usr/local/nginx/conf/fcgi.conf #添加:
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
六、创建虚拟主机配置文件
vi /usr/local/nginx/conf/vhost/bbs..conf #添加以下内存
{
listen 80;
server_name bbs.;
index index.php index.html index.htm default.html default.htm default.php;
root /data/xxx/bbs;
location ~ .*\.(php|php5)?$
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
location /status {
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
access_log off;
}
vi /usr/local/nginx/conf/vhost/sns..conf #添加:
{
listen 80;
server_name sns.;
index index.php index.html index.htm default.html default.htm default.php;
root /data/xxx/sns;
location ~ .*\.(php|php5)?$
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
location /status {
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
access_log off;
}
七、测试
vi /data/xxx/bbs/index.php #新建文件,添加:
phpinfo();
?>
:wq! #保存退出
vi /data/xxx/sns/index.php #新建文件,添加:
phpinfo();
?>
:wq! #保存退出
/etc/rc.d/init.d/php-fpm restart #重启php-fpm
测试,打开:
http://bbs./
您可能感兴趣的文章:
nginx虚拟主机配置与原理解析
一个生产环境下的nginx.conf配置文件(多虚拟主机)
windows nginx 多站点虚拟主机配置教程
nginx 虚拟主机设置一例
Apache中虚拟主机设置泛域名解析一例
nginx创建反向代理和虚拟主机的例子
Nginx虚拟主机配置范例代码