推荐一本关于Nginx的书
作者是:Kevin(kevin.zwf#gmail.com) & Mark(kissingwolf@gmail.com)
地址:http://wenku.baidu.com/view/e9763a23482fb4daa58d4b29.html
有比较详细的介绍,以及实例~
作者是:Kevin(kevin.zwf#gmail.com) & Mark(kissingwolf@gmail.com)
地址:http://wenku.baidu.com/view/e9763a23482fb4daa58d4b29.html
有比较详细的介绍,以及实例~
整了一个域名,涉及到将二级域名为数字的转到特定的目录下,这里用nginx的匹配$host来实现
下面是一个简单的配置,请看
#绑定域名
server_name *.abcd.com;
#匹配你的二级域名
if ( $host ~* (.*)\.yourdomain\.com)
{
set $domain $1;
}
#定义目录
root html/abc/$domain/;
location /
{
root html/abcd/$domain;
index index.html index.php;
}
注:在Nginx下,expires和防盗链(valid_referers)一起用的时候,会出现防盗链失效的情况!其详细状况如下:
expires有效,防盗链失效
location ~* ^.+\.(jpg|jpeg|gif|png|css|js|swf)$ {
access_log off;
root /opt/htdocs/career;
expires 1h;
#break;
}
location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip)$ {
valid_referers none blocked *.c1gstudio.com;
if ($invalid_referer) {
rewrite ^/ http://leech.c1gstudio.com/leech.gif;
return 412;
}
}
只有js和css的expire有效,防盗链有效
location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip)$ {
valid_referers none blocked *.c1gstudio.com;
if ($invalid_referer) {
rewrite ^/ http://leech.c1gstudio.com/leech.gif;
return 412;
}
}
location ~* ^.+\.(jpg|jpeg|gif|png|css|js|swf)$ {
access_log off;
root /opt/htdocs/career;
expires 1h;
#break;
}
让expire和防盗链都有效
location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
valid_referers none blocked *.c1gstudio.com;
if ($invalid_referer) {
rewrite ^/ http://leech.c1gstudio.com/leech.gif;
return 412;
}
access_log off;
root /opt/htdocs/career;
expires 1h;
break;
}
来源:http://www.ccvita.com/319.html
Nginx的Rewrite
经过网上查阅和测试,发现Nginx的Rewrite规则和Apache的Rewite规则差别不是很大,几乎可以直接使用。比如在Apache中这样写规则
rewrite ^/([0-9]{5}).html$ /viewthread.php?tid=$1 last;
而在Nginx中写成这样写是无法启动的,解决的办法是加上两个双引号:
rewrite “^/([0-9]{5}).html$” /viewthread.php?tid=$1 last;
同时将RewriteRule为Rewrite,基本就实现了Nginx的Rewrite规则到Apache的Rewite规则的转换。
Rewrite的Flags
last – 基本上都用这个Flag。
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301
官方文档请点击这里,另外如果对于302,301这些状态有疑问的,可以参考《301 Redirect 永久重定向的实现》:http://www.ccvita.com/85.html
如果需要对Nginx配置防盗链的话,可以参考《Nginx的防盗链配置》:http://www.ccvita.com/312.html
Discuz!在Nginx下的Rewrite
需要说明的是,下网上以前一直流传的Rewrite都是有误的。
下面的Rewrite中百分号前面多了个转移字符“\”,这在Apache中是需要的,而在Nginx中则是不需要的。
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page\%3D$3&page=$2 last;
正确的应该是
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;
这个错误在基本上目前所有使用Nginx作为服务器,并且开启了Rewrite的网站上存在。包括Discuz!官方,目前已经给cnteacher反馈了。
完整正确的Discuz!在Nginx下的Rewrite如下:
rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$ /archiver/index.php?$1 last;
rewrite ^/forum-([0-9]+)-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2 last;
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;
rewrite ^/space-(username|uid)-(.+)\.html$ /space.php?$1=$2 last;
rewrite ^/tag-(.+)\.html$ /tag.php?name=$1 last;
break;
相关文档:
《Nginx下Discuz!的Rewrite》:http://www.ccvita.com/348.html
《Nginx下WordPress的Rewrite》:http://www.ccvita.com/336.html
《Nginx的Rewrite配置》:http://www.ccvita.com/319.html
《Nginx的防盗链配置》:http://www.ccvita.com/312.html
来源:http://blog.licess.org/nginx-autoindex/
Nginx默认是不允许列出整个目录的。如需此功能,
打开nginx.conf文件,在location server 或 http段中加入
autoindex on;
另外两个参数最好也加上去:
autoindex_exact_size off;
默认为on,显示出文件的确切大小,单位是bytes。
改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
autoindex_localtime on;
默认为off,显示的文件时间为GMT时间。
改为on后,显示的文件时间为文件的服务器时间
location /images {
root /var/www/nginx-default/ibugaocn;
autoindex on;
}
详细参照:http://wiki.nginx.org/NginxChsHttpAutoindexModule
如果想希望目录列表支持header,footer则可以安装三方插件:
http://wiki.nginx.org/NginxNgxFancyIndex
来源:http://www.sudone.com/nginx/nginx_vs_lvs.html
在一个网站中往往图片,css文件,js文件会占用掉大量的带宽和载入时间.采用nginx做前端服务器可以设置类似的静态文件客户端的缓存时间.比如:
location ~ \.(gif|jpg|jpeg|png|bmp|ico|swf|css|js)$ {
expires 30d;
access_log off;
}
就可以将类似静态文件的客户端缓存时间设置为30天,意味着客户在30天内重新访问这些文件时只需要在本地缓存中读取,而不用重新从服务器获取.这样页面载入速度就大大提高了.
当然,对于这些静态文件的访问记录计入日志,在一般情况下也是没有意义的,将accss_log设为off,能在一定程度上降低服务器压力.
apt-get install mysql-server
apt-get install nginx
deb http://php53.dotdeb.org stable alldeb-src http://php53.dotdeb.org stable all
apt-get updateapt-get install php5-cgi php5-fpm
php-cgi –i
## nginx-site-conf.sample:# Php Site configuration for nginx webserver## 1. set server root /path/to/your/website;# 2. Rename this file. Copy it to /etc/nginx/sites-available, /etc/nginx/sites-enabled# or otherwise ensure that this file is included by the nginx.conf# 3. Restart nginx webserver, and php-fpm service.#server {root /home/dawnh/opslife.com;server_name opslife.com www.opslife.com d9.opslife.com;listen 80;access_log /var/log/nginx/opslife.com.access.log;location / {index index.html index.htm index.php;}#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 /var/www/nginx-default;}# pass the *.php scripts to php-fpm listening on tcp port 9000#location ~ \.php$ {fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;include fastcgi_params;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param SERVER_NAME $http_host;fastcgi_ignore_client_abort on;}}
<?php phpinfo();?>
来源:未知,如有了解,请告知,谢谢
upstream bakend {server 192.168.0.14 weight=10;server 192.168.0.15 weight=10;}
upstream bakend {ip_hash;server 192.168.0.14:88;server 192.168.0.15:80;}
upstream backend {server server1;server server2;fair;}
upstream backend {server squid1:3128;server squid2:3128;hash $request_uri;hash_method crc32;}
upstream bakend{#定义负载均衡设备的Ip及设备 状态ip_hash;server 127.0.0.1:9090 down;server 127.0.0.1:8080 weight=2;server 127.0.0.1:6060;server 127.0.0.1:7070 backup;}
proxy_pass http://bakend/;
来自:http://shiningray.cn/nginx-de-wordpress-pei-zhi.html
WordPress是一个非常流行的Blog系统,它可以利用Apache的mod_rewrite来实现URL的静态化。安装好的WordPress在配置了持久链接之后,会在网站的根目录下(如果可写)生成一个.htaccess文件,这个文件可以指示Apache如何进行URL重写(如果服务器配置为允许使用htaccess的指令的话),它的内容如下:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
这个文件的意思就是,如果当请求的文件不存在,那么把请求内部重定向到/index.php。WordPress会自己分析请求的URL,来判断显示哪个页面。
在上次配置了Nginx+PHP之后,由于Nginx不支持Apache的.htaccess文件,要实现持久连接静态化,我们必须手工配置Nginx的文件。首先找到Nginx的配置文件,默认编译后的配置文件在/usr/local/nginx/conf/nginx.conf;Ubuntu通过包安装的配置文件位于/etc/nginx/nginx.conf,也可以编辑vhost的配置文件,放在了/etc/nginx/sites-available下。
以下是基本的配置(Ubuntu下的范例):
location / {
index index.html index.php;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
location ~ .*\.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
还可以有很多种不同配置方式,例如不改写所有包含wp-的url等。此配置考虑了目录下的索引文件index.html和index.php。-f指令表示测试文件是否存在(不考虑文件和目录的区别),!-f则表示不存在。注意在重写url到index.html后面有个break,而重写到index.php后没有break。因为html文件不需要任何额外工作可以直接发送到客户端,所以重写规则在这里终止,下面就直接让nginx发送文件。而.php文件需要进一步发送到fastcgi进程来运行,Nginx会继续判断该文件符合第二个部分location ~ .*\.php$的规则,并进行FastCGI的转发。
大家可以将以上内容保存为wordpress.conf,然后在自己的vhost配置,即server节中应用该配置文件,例如(以下为Ubuntu进行的配置):
server {
listen 80;
server_name shiningray.cn *.shiningray.cn;
root /var/www/shiningray.cn;
include /etc/nginx/wordpress.conf;
}
接下来让Nginx重新载入配置文件,便可使用WordPress的持久链接了。