独学而无友,则孤陋而寡闻!
分类: 系统运维
2009-10-13 18:24:53
CODE:
./configure
make && make install
cd ../
2. nginx 编译安装CODE:
./configure --user=www --group=www --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-openssl=/usr/local/openssl
make && make install
更详细的模块定制与安装请参照官方wiki. CODE:
# /usr/local/nginx/sbin/nginx -t //Debug 配置文件的关键命令需要重点撑握.
2008/12/16 09:08:35 [info] 28412#0: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
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
$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
四、 Nginx RedirectCODE:
server
{
listen 80;
server_name linuxtone.org netseek.linuxtone.org;
index index.html index.php;
root /data/www/wwwroot;
if ($host !~ "^www\.linxtone\.org$") {
rewrite ^(.*) redirect;
}
........................
}
五、 Nginx 目录自动加斜线:CODE:
if (-d $request_filename){
rewrite ^/(.*)([^/])$ permanent;
}
六 Nginx LocationCODE:
# Add expires header for static content
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
root /data/www/wwwroot/bbs;
expires 1d;
break;
}
}
2、根据判断某个目录CODE:
# serve static files
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /data/www/wwwroot/down;
expires 30d;
}
八、 Nginx 防盗链CODE:
#Preventing hot linking of images and other file types
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
valid_referers none blocked server_names *.linuxtone.org linuxtone.org baidu.com;
if ($invalid_referer) {
rewrite ^/ ;
# return 403;
}
}
2. 针对不同的目录CODE:
location /img/ {
root /data/www/wwwroot/bbs/img/;
valid_referers none blocked server_names *.linuxtone.org baidu.com;
if ($invalid_referer) {
rewrite ^/ ;
#return 403;
}
}
3. 同实现防盗链和expires的方法CODE:
#Preventing hot linking of images and other file types
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
valid_referers none blocked server_names *.linuxtone.org linuxtone.org ;
if ($invalid_referer) {
rewrite ^/ ;
}
access_log off;
root /data/www/wwwroot/bbs;
expires 1d;
break;
}
九、 Nginx 访问控制CODE:
#cd /usr/local/nginx/conf
#mkdir htpasswd
/usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/htpasswd/tongji linuxtone
#添加用户名为linuxtone
New password: (此处输入你的密码)
Re-type new password: (再次输入你的密码)
Adding password for user
(目录存在/data/www/wwwroot/tongji/data/目录下)
将下段配置放到虚拟主机目录,当访问即提示要密验证:
location ~ ^/(tongji)/ {
root /data/www/wwwroot/count;
auth_basic "LT-COUNT-TongJi";
auth_basic_user_file /usr/local/nginx/conf/htpasswd/tongji;
}
2. Nginx 禁止访问某类型的文件.CODE:
location ~* \.(txt|doc)$ {
if (-f $request_filename) {
root /data/www/wwwroot/linuxtone/test;
#rewrite …..可以重定向到某个URL
break;
}
}
方法2:CODE:
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test;
deny all;
}
实例:CODE:
location ~ ^/(WEB-INF)/ {
deny all;
}
3. 使用ngx_http_access_module限制ip访问CODE:
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
deny all;
}
详细参见wiki: CODE:
limit_zone linuxtone $binary_remote_addr 10m;
server
{
listen 80;
server_name down.linuxotne.org;
index index.html index.htm index.php;
root /data/www/wwwroot/down;
#Zone limit
location / {
limit_conn linuxtone 1;
limit_rate 20k;
}
..........
}
只允许客房端一个线程,每个线程20k.CODE:
location / {
autoindex on;
}
6. 上文件大小限制CODE:
#!/bin/bash
log_dir="/data/logs"
time=`date +%Y%m%d`
/bin/mv ${log_dir}/access_linuxtone.org.log ${log_dir}/access_count.linuxtone.org.$time.log
kill -USR1 `cat /var/run/nginx.pid`
更多的日志分析与处理就关注(同时欢迎你参加讨论):CODE:
proxy_store on;
proxy_store_access user:rw group:rw all:rw;
proxy_temp_path 缓存目录;
其中,CODE:
proxy_pass:
if ( !-e $request_filename) {
proxy_pass
}
即改成有条件地去执行proxy_pass,这个条件就是当请求的文件在本地的proxy_temp_path指定的目录下不存在时,再向后端拉取。CODE:
upstream bbs.linuxtone.org {#定义负载均衡设备的Ip及设备状态
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;
}
在需要使用负载均衡的server中增加CODE:
……….
#loadblance my.linuxtone.org
upstream my.linuxtone.org {
ip_hash;
server 127.0.0.1:8080;
server 192.168.169.136:8080;
server 219.101.75.138:8080;
server 192.168.169.117;
server 192.168.169.118;
server 192.168.169.119;
}
…………..
include vhosts/linuxtone_lb.conf;
………
# vi proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
proxy_store on;
proxy_store_access user:rw group:rw all:r;
#nginx cache
#client_body_temp_path /data/nginx_cache/client_body 1 2;
proxy_temp_path /data/nginx_cache/proxy_temp 1 2;
#vi linuxtone_lb.confCODE:
server
{
listen 80;
server_name my.linuxtone.org;
index index.php;
root /data/www/wwwroot/mylinuxtone;
if (-f $request_filename) {
break;
}
if (-f $request_filename/index.php) {
rewrite (.*) $1/index.php break;
}
error_page 403
location / {
if ( !-e $request_filename) {
proxy_pass
break;
}
include /usr/local/nginx/conf/proxy.conf;
}
}
CODE:
CFLAGS=”$CFLAGS -g”
注释掉或删掉这几行,重新编译即可。CODE:
#vi nginx-0.7.30/src/core/nginx.h
#define NGINX_VERSION "1.8"
#define NGINX_VER "LTWS/" NGINX_VERSION
#define NGINX_VAR "NGINX"
#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[] =
"
" NGINX_VER " " CRLF
"
" CRLF
"