[@more@]
Nginx安装:
]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/bin/ --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
]# make
]# make install
]# /usr/local/bin/nginx -V
nginx: nginx version: nginx/1.0.4
nginx: TLS SNI support disabled
nginx: configure arguments: --prefix=/usr/local/src --sbin-path=/usr/local/bin/ --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
]#
应养成检查语法的习惯,如下例:
]# /usr/local/bin/nginx -t
nginx: the configuration file /usr/local/src/conf/nginx.conf syntax is ok
nginx: [emerg] getpwnam("www") failed
nginx: configuration file /usr/local/src/conf/nginx.conf test failed
]# useradd www -s /sbin/nologin -M
]# /usr/local/bin/nginx -t
nginx: the configuration file /usr/local/src/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/src/conf/nginx.conf test is successful
启动nginx:
]# /usr/local/bin/nginx -c /usr/local/nginx/conf/nginx.conf
]# !netstat
netstat -natu | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
nginx主配置文件:
[root@up_server conf]# pwd
/usr/local/nginx/conf
[root@up_server conf]# ls nginx.conf
nginx.conf
重启nginx服务命令
]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
]# /usr/local/bin/nginx -s reload
error_page 404 /404.html; 为了界面友好
error_page 500 502 503 504 /50x.html; 出现上面错误码,跳转后面界面
虚拟主机:
(1)基于域名:
server {
listen 80;
server_name www.sina.com;
location / {
root sina;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.baidu.com;
location / {
root baidu;
index index.html index.htm;
}
}
(2)基于IP
server {
listen 192.168.1.25:80;
server_name 192.168.1.25;
location / {
root 254;
index index.html index.htm;
}
}
ab -c 100 n 10000 http://www.baidu.com 并发10000次,每次100个请求
正则匹配:
location ~* .*.doc$ {
66 #location ~ .*/.*.(txt|doc) {
67 return 403;
68 #root 254;
69 #deny all;
70 }
location ~* .*.doc$ {
66 #location ~ .*/.*.(txt|doc) {
67 root 254;
68 deny all;
69 }
location /nginx_status {
66 stub_status on;
67 allow 192.168.1.2;
68 deny all;
69 }
此时用1.254来访问会返回403错误信息
对于图片的访问不计入日志,没有必要,图片太多,避免日志过大
location ~ .*.(jpg|png|css|js|gif)$ {
access_log off;
}
在nginx中作日志截取
1.写一个脚本 利用mv 笨方法
2.计划任务 分时日月周
[root@ disk]# cat cut_nginx.sh
#!/bin/bash
# This script run at 00:00
# The Nginx logs path
logs_path="/usr/local/webserver/nginx/logs/"
mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/webserver/nginx/nginx.pid`
[root@ disk]#
设置访问权限:
location / {
root abc; 访问根目录
index index.html index.htm;
auth_basic "这是我私有的";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
[root@wang src]# htpasswd -cm /usr/local/src/conf/htpasswd sungy
New password:
Re-type new password:
Adding password for user sungy
[root@wang src]#
再补充一些关于location正则和rewrite相关的学习
nginx反向代理 重点
1。默认是轮询的 (rr)
keepalive_timeout 65;
upstream server_pool {
server 192.168.1.23:80;
server 192.168.1.25:80;
}
#gzip on;
server {
listen 80;
server_name www.baidu.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root baidu;
#index index.html index.htm;
proxy_pass http://server_pool;
??proxy_set_header Host $host;
}
2。带有权重(wrr)的集群
keepalive_timeout 65;
upstream server_pool {
server 192.168.1.23:80 weight=2 max_fails=2 fails_timeout=30; #2次失败以后,暂停的时间30sOB
server 192.168.1.25:80;
}
#gzip on;
server {
listen 80;
server_name www.baidu.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root baidu;
#index index.html index.htm;
proxy_pass http://server_pool;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr; 可以看到访问客户的真实IP
}
3.hash 解决购物车问题
4.根据服务器新旧
5.。基于url的hash
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23168012/viewspace-1052518/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/23168012/viewspace-1052518/