CODE:
./configure<br> make && make install<br> cd ../
2. nginx 编译安装CODE:
./configure --user=www --group=www --prefix=/usr/local/nginx/
--with-http_stub_status_module --with-openssl=/usr/local/openssl <br>make && make install
更详细的模块定制与安装请参照官方wiki. CODE:
# /usr/local/nginx/sbin/nginx -t //Debug 配置文件的关键命令需要重点撑握.<br>2008/12/16 09:08:35 [info] 28412#0: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok <br>2008/12/16 09:08:35 [info] 28412#0: the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
3、Nginx 启动:CODE:
# /usr/local/nginx/sbin/nginx
4、Nginx
配置文件修改重新加载:CODE:
# kill -HUP `cat /usr/local/nginx/logs/nginx.pid
`CODE:
$args<br>$content_length<br>$content_type<br>$document_root<br>$document_uri<br>$host<br>$http_user_agent<br>$http_cookie<br>$limit_rate<br>$request_body_file<br>$request_method<br>$remote_addr<br>$remote_port<br>$remote_user<br>$request_filename<br>$request_uri<br>$query_string<br>$scheme<br>$server_protocol<br>$server_addr<br>$server_name<br>$server_port<br>$uri
四、 Nginx RedirectCODE:
server<br>{<br>listen 80;<br>server_name linuxtone.org netseek.linuxtone.org;<br>index index.html index.php;<br>root /data/www/wwwroot;<br>if ($host !~ "^www\.linxtone\.org$") {<br>rewrite ^(.*) http://www.linuxtone.org$1 redirect;<br>}<br>........................<br>}
五、 Nginx 目录自动加斜线:CODE:
if (-d $request_filename){<br> rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;<br> }
六 Nginx LocationCODE:
# Add expires header for static content<br>location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {<br> if (-f $request_filename) {<br> root /data/www/wwwroot/bbs;<br> expires 1d;<br> break;<br> }<br>}
2、根据判断某个目录CODE:
# serve static files<br>location ~ ^/(images|javascript|js|css|flash|media|static)/ {<br>root /data/www/wwwroot/down;<br> expires 30d;<br> }
八、 Nginx 防盗链CODE:
#Preventing hot linking of images and other file
types<br>location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {<br> valid_referers none blocked server_names *.linuxtone.org linuxtone.org http://localhost baidu.com;<br>if ($invalid_referer) {<br> rewrite ^/ ;<br> # return 403;<br> }<br>}
2. 针对不同的目录CODE:
location /img/ {<br> root /data/www/wwwroot/bbs/img/;<br> valid_referers none blocked server_names *.linuxtone.org http://localhost baidu.com;<br> if ($invalid_referer) {<br> rewrite ^/ ;<br> #return 403;<br> }<br>}
3. 同实现防盗链和expires的方法CODE:
#Preventing hot linking of images and other file
types<br>location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {<br> valid_referers none blocked server_names *.linuxtone.org linuxtone.org http://localhost ;<br>if ($invalid_referer) {<br> rewrite ^/ ;<br> }<br> access_log off;<br> root /data/www/wwwroot/bbs;<br>expires 1d;<br> break;<br>}
九、 Nginx 访问控制CODE:
#cd /usr/local/nginx/conf<br>#mkdir htpasswd<br>/usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/htpasswd/tongji linuxtone <br>#添加用户名为linuxtone<br>New password: (此处输入你的密码)<br>Re-type new password: (再次输入你的密码)<br>Adding password for user<br>http://count.linuxtone.org/tongji/data/index.html(目录存在/data/www/wwwroot/tongji/data/目录下)<br>将下段配置放到虚拟主机目录,当访问http://count.linuxtone/tongji/即提示要密验证:<br>location ~ ^/(tongji)/ {<br> root /data/www/wwwroot/count;<br> auth_basic "LT-COUNT-TongJi";<br> auth_basic_user_file /usr/local/nginx/conf/htpasswd/tongji;<br> }
2. Nginx 禁止访问某类型的文件.CODE:
location ~* \.(txt|doc)$ {<br> if (-f $request_filename) {<br> root /data/www/wwwroot/linuxtone/test;<br> #rewrite …..可以重定向到某个URL<br> break;<br> }<br>}
方法2:CODE:
location ~* \.(txt|doc)${<br> root /data/www/wwwroot/linuxtone/test;<br> deny all;<br>}
实例:CODE:
location ~ ^/(WEB-INF)/ { <br> deny all; <br>}
3. 使用ngx_http_access_module限制ip访问CODE:
location / {<br> deny 192.168.1.1;<br> allow 192.168.1.0/24;<br> allow 10.1.1.0/16;<br> deny all;<br>}
详细参见wiki: http://wiki.codemongers.com/NginxHttpAccessModule#allowCODE:
limit_zone linuxtone $binary_remote_addr 10m;<br>server<br> {<br> listen 80;<br> server_name down.linuxotne.org;<br> index index.html index.htm index.php;<br> root /data/www/wwwroot/down;<br> #Zone limit<br> location / {<br> limit_conn linuxtone 1;<br> limit_rate 20k;<br> }<br>..........<br> }
只允许客房端一个线程,每个线程20k.CODE:
location / {<br> autoindex on;<br>}
6. 上文件大小限制CODE:
#!/bin/bash<br>log_dir="/data/logs"<br>time=`date +%Y%m%d` <br>/bin/mv ${log_dir}/access_linuxtone.org.log ${log_dir}/access_count.linuxtone.org.$time.log<br>kill -USR1 `cat /var/run/nginx.pid`
更多的日志分析与处理就关注(同时欢迎你参加讨论):http://bbs.linuxtone.org/forum-8-1.htmlCODE:
proxy_store on;<br>proxy_store_access user:rw group:rw all:rw;<br>proxy_temp_path 缓存目录;
其中,CODE:
proxy_pass:<br>if ( !-e $request_filename) {<br> proxy_pass http://mysvr;<br>}
即改成有条件地去执行proxy_pass,这个条件就是当请求的文件在本地的proxy_temp_path指定的目录下不存在时,再向后端拉取。CODE:
upstream bbs.linuxtone.org {#定义负载均衡设备的Ip及设备状态<br> server 127.0.0.1:9090 down;<br> server 127.0.0.1:8080 weight=2;<br> server 127.0.0.1:6060;<br> server 127.0.0.1:7070 backup;<br>}
在需要使用负载均衡的server中增加CODE:
……….<br>#loadblance my.linuxtone.org<br> upstream my.linuxtone.org {<br> ip_hash;<br> server 127.0.0.1:8080;<br> server 192.168.169.136:8080;<br> server 219.101.75.138:8080;<br> server 192.168.169.117;<br> server 192.168.169.118;<br> server 192.168.169.119;<br> }<br>…………..<br>include vhosts/linuxtone_lb.conf;<br>………<br># vi proxy.conf<br>proxy_redirect off;<br>proxy_set_header Host $host;<br>proxy_set_header X-Real-IP $remote_addr;<br>proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br>client_max_body_size 50m;<br>client_body_buffer_size 256k;<br>proxy_connect_timeout 30;<br>proxy_send_timeout 30;<br>proxy_read_timeout 60;<br>proxy_buffer_size 4k;<br>proxy_buffers 4 32k;<br>proxy_busy_buffers_size 64k;<br>proxy_temp_file_write_size 64k;<br>proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;<br>proxy_max_temp_file_size 128m;<br>proxy_store on;<br>proxy_store_access user:rw group:rw all:r;<br>#nginx cache <br>#client_body_temp_path /data/nginx_cache/client_body 1 2;<br>proxy_temp_path /data/nginx_cache/proxy_temp 1 2;
#vi linuxtone_lb.confCODE:
server<br> {<br> listen 80;<br> server_name my.linuxtone.org;<br> index index.php;<br> root /data/www/wwwroot/mylinuxtone;<br> if (-f $request_filename) {<br> break;<br> }<br> if (-f $request_filename/index.php) {<br> rewrite (.*) $1/index.php break;<br> }<br> error_page 403 http://my.linuxtone.org/member.php?m=user&a=login;<br> location / {<br> if ( !-e $request_filename) {<br> proxy_pass http://my.linuxtone.org;<br> break;<br> }<br> include /usr/local/nginx/conf/proxy.conf;<br> }<br>}
CODE:
CFLAGS=”$CFLAGS -g”
注释掉或删掉这几行,重新编译即可。CODE:
#vi nginx-0.7.30/src/core/nginx.h<br>#define NGINX_VERSION "1.8"<br>#define NGINX_VER "LTWS/" NGINX_VERSION<br>#define NGINX_VAR "NGINX"<br>#define NGX_OLDPID_EXT ".oldbin"
2) 修改nginx_http_header_filter_moduleCODE:
static char ngx_http_server_string[] = "Server:
nginx" CRLF;
修改为CODE:
static char ngx_http_server_string[] = "Server:
LTWS" CRLF;
a) 修改nginx_http_header_filter_moduleCODE:
static u_char ngx_http_error_full_tail[] =<br>"
CODE:
static u_char ngx_http_error_tail[] =<br>"
CODE:
static u_char ngx_http_error_full_tail[] =<br>"
404.png
curl.png
CODE:
# wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99-alpha.tar.gz<br># tar zxvf libunwind-0.99-alpha.tar.gz<br># cd libunwind-0.99-alpha/<br># CFLAGS=-fPIC ./configure<br># make CFLAGS=-fPIC<br># make CFLAGS=-fPIC install<br># wget http://google-perftools.googlecode.com/files/google-perftools-0.98.tar.gz<br># tar zxvf google-perftools-0.98.tar.gz<br># cd google-perftools-0.98/<br># ./configure<br># make && make install<br># echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf<br># ldconfig<br># lsof -n | grep tcmalloc
编译nginx 加载google_perftools_module:CODE:
net.ipv4.tcp_fin_timeout = 30<br>net.ipv4.tcp_keepalive_time = 300<br>net.ipv4.tcp_syncookies = 1<br>net.ipv4.tcp_tw_reuse = 1<br>net.ipv4.tcp_tw_recycle = 1<br>net.ipv4.ip_local_port_range = 5000 65000
#使配置立即生效CODE:
#mkdir -p /data/www/wwwroot/nginx/{rrd,html}<br>#cd /usr/local/sbin<br>#wget http://www.nginx.eu/nginx-rrd/nginx-rrd-0.1.4.tgz<br>#tar zxvf nginx-rrd-0.1.4.tgz<br>#cd nginx-rrd-0.1.4<br>#cd etc/<br>#cp nginx-rrd.conf /etc<br>#cd etc/cron.d<br>#cp nginx-rrd.cron /etc/cron.d<br>#cd /usr/local/src/nginx-rrd-0.1.4/html<br># cp index.php /data/www/wwwroot/nginx/html/<br>#cd /usr/local/src/nginx-rrd-0.1.4/usr/sbin<br>#cp * /usr/sbin/
#vi /etc/nginx-rrd.confCODE:
#####################################################<br>#<br># dir where rrd databases are stored<br>RRD_DIR="/data/www/wwwroot/nginx/rrd";<br># dir where png images are presented<br>WWW_DIR="/data/www/wwwroot/nginx/html";<br># process nice level<br>NICE_LEVEL="-19";<br># bin dir<br>BIN_DIR="/usr/sbin";<br># servers to test<br># server_utl;server_name<br>SERVERS_URL="http://219.32.205.13/nginx_status;219.32.205.13 http://www.linuxtone.org/nginx_status;www.linuxtone.org""
//根据你的具体情况做调整.CODE:
location /nginx_status {<br>stub_status on;<br>access_log off;<br>allow 192.168.1.37;<br>deny all;<br>}
CODE:
# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`<br># wget http://forums.cacti.net/download.php?id=12676<br># tar xvfz cacti-nginx.tar.gz<br># cp cacti-nginx/get_nginx_socket_status.pl /data/cacti/scripts/<br># cp cacti-nginx/get_nginx_clients_status.pl /data/cacti/scripts/<br># chmod 755 /data/cacti/scripts/get_nginx*
检测插件CODE:
# /data/cacti/scripts/get_nginx_clients_status.pl
http://192.168.1.37/nginx_status
在cacti管理面板导入CODE:
server {<br>listen 80;<br>server_name java.linuxtone.org<br>location / {<br>proxy_pass http://192.168.1.2:8080;<br>include /usr/local/nginx/conf/proxy.conf;<br>}<br>}
6、如何关闭Nginx的LOG以上就介绍了Nginx 常见应用技术指南[Nginx Tips] 第二版,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。