分类: LINUX
2010-06-24 10:07:00
前面总结了几篇nginx的配置文档,但忽略了一些常用的配置,例如访问控制,目录索引,重定向等,因而需要在此记录下,方便熟悉nginx,在此总结的内容也比较肤浅,刚开始学习。
1:针对主机的访问控制
[root@yunwei ~]# grep -A 3 -B 3 '192.168.50.40' /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
allow 192.168.50.40; //支持整个网段,192.168.50.0/24这样的语法
deny all;
}
[root@yunwei ~]# /usr/local/nginx/sbin/nginx -t
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@yunwei ~]# tail -f /usr/local/nginx/logs/access.log
192.168.50.40 - - [21/Jun/2010:15:42:58 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; 360SE)"
[root@yunwei ~]# tail -f /usr/local/nginx/logs/error.log
2010/06/21 15:43:53 [error] 21164#0: *15 access forbidden by rule, client: 192.168.50.27, server: localhost, request: "GET / HTTP/1.1", host: "192.168.50.3"
2:针对用户的访问控制
[root@yunwei ~]# grep -A 5 -B 5 'auth' /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
allow 192.168.50.0/24;
deny all;
auth_basic "test";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
}
error_page 404 /404.html;
[root@yunwei ~]# /usr/local/apache2.2.15/bin/htpasswd -cd /usr/local/nginx/conf/.htpasswd yang
New password:
Re-type new password:
Adding password for user yang
3:开启自动索引功能
[root@yunwei ~]# grep -A 3 -B 3 'autoindex' /usr/local/nginx/conf/nginx.conf
location / {
root html;
autoindex on;
index index.html index.htm;
allow 192.168.50.0/24;
deny all;
4:整合ZendOptimizer
[root@yunwei ~]# cd /usr/local/src/tarbag/
[root@yunwei tarbag]# wget
[root@yunwei tarbag]# tar -zxvf ZendOptimizer-3.3.9-linux-glibc23-x86_64.tar.gz -C ../software/
[root@yunwei tarbag]# mv ../software/ZendOptimizer-3.3.9-linux-glibc23-x86_64/ /usr/local/Zend3
[root@yunwei tarbag]# grep -i 'zend3' /usr/local/php5.2.13/etc/php.ini |grep -v ';'
zend_extension=/usr/local/Zend3/data/5_2_x_comp/ZendOptimizer.so
[root@yunwei tarbag]# service php-fpm restart
Shutting down php_fpm . done
Starting php_fpm done
[root@yunwei tarbag]# service nginx restart
nginx is restarted...
[root@yunwei tarbag]# /usr/local/php5.2.13/bin/php -m |grep -i zend
[Zend Modules]
Zend Optimizer
5: 整合memcached
[root@yunwei ~]# cd /usr/local/src/tarbag/
[root@yunwei tarbag]# wget
[root@yunwei tarbag]# tar -zxvf memcache-2.2.5.tgz -C ../software/
[root@yunwei tarbag]# cd ../software/memcache-2.2.5/
[root@yunwei memcache-2.2.5]# /usr/local/php5.2.13/bin/phpize
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519
[root@yunwei memcache-2.2.5]# ./configure --enable-memcache --with-php-config=/usr/local/php5.2.13/bin/php-config --with-zlib-dir
[root@yunwei memcache-2.2.5]# make && make install
………………………………………………输出省略………………………………………………
Installing shared extensions: /usr/local/php5.2.13/lib/php/extensions/no-debug-non-zts-20060613/
[root@yunwei memcache-2.2.5]# grep -A 2 'extension_dir' /usr/local/php5.2.13/etc/php.ini |grep -v ';'
extension_dir = "/usr/local/php5.2.13/lib/php/extensions/no-debug-non-zts-20060613/"
extension=memcache.so
[root@yunwei memcache-2.2.5]# service php-fpm restart
Shutting down php_fpm . done
Starting php_fpm done
[root@yunwei memcache-2.2.5]# service nginx restart
nginx is restarted...
[root@yunwei memcache-2.2.5]# /usr/local/php5.2.13/bin/php -m |grep mem
memcache
6: 重定向
[root@yunwei ~]# grep -A 3 -B 8 'permanent' /usr/local/nginx/conf/nginx.conf
location / {
root html;
autoindex on;
index index.html index.htm;
allow 192.168.50.0/24;
deny all;
auth_basic "test";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
rewrite /download permanent;
}
error_page 404 /404.html;
[root@yunwei ~]# tail -f /usr/local/nginx/logs/access.log
192.168.50.40 - - [21/Jun/2010:09:57:13 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; 360SE)"