nginx這裏就不作說明了,都知道nginx中最大的亮點是反向代理和靜態頁面的響應,固然其餘的功能也不錯,下面將介紹下nginx的一些功能的使用詳解,
*********************************************************************************************************************************************
目錄:
一, 軟件安裝
二, 虛擬主機
三, 用戶認證
四, HTTPS
五, 反向代理
六, 負載均衡
七, URL重寫php
*********************************************************************************************************************************************html
在rhel5.x上實現以上功能node
*********************************************************************************************************************************************
一,編譯安裝nginxnginx
- 下載nginx-1.2.2.tar.gz到/usr/src目錄 固然也能夠下載最新版的,1.2.2的版本目前來講仍是最新版的
- #安裝相關軟件包
- #yum -y groupinstall "Development Libraries" "Development Tools"
- #yum -y install pcre-devel
- #useradd -s /sbin/nologin nginx
- #cd /usr/src
- #tar xzvf nginx-1.2.2.tar.gz
- #cd /usr/src/nginx-1.2.2
- #./configure --prefix=/usr/local/nginx --pid-path=/var/run/nginx/nginx.pid \
- --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module \
- --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module \
- --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy \
- --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
- --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre
- #make && make install
給nginx提供SysV服務啓動腳本正則表達式
- #vim /etc/init.d/nginx 內容以下
- #!/bin/sh
- #
- # nginx - this script starts and stops the nginx daemon
- #
- # chkconfig: - 85 15
- # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
- # proxy and IMAP/POP3 proxy server
- # processname: nginx
- # config: /usr/local/nginx/conf/nginx.conf
- # config: /etc/sysconfig/nginx
- # pidfile: /var/run/nginx.pid
- # Source function library.
- . /etc/rc.d/init.d/functions
- # Source networking configuration.
- . /etc/sysconfig/network
- # Check that networking is up.
- [ "$NETWORKING" = "no" ] && exit 0
- nginx="/usr/local/nginx/sbin/nginx"
- prog=$(basename $nginx)
- NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
- [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
- lockfile=/var/lock/subsys/nginx
- make_dirs() {
- # make required directories
- user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
- options=`$nginx -V 2>&1 | grep 'configure arguments:'`
- for opt in $options; do
- if [ `echo $opt | grep '.*-temp-path'` ]; then
- value=`echo $opt | cut -d "=" -f 2`
- if [ ! -d "$value" ]; then
- # echo "creating" $value
- mkdir -p $value && chown -R $user $value
- fi
- fi
- done
- }
- start() {
- [ -x $nginx ] || exit 5
- [ -f $NGINX_CONF_FILE ] || exit 6
- make_dirs
- echo -n $"Starting $prog: "
- daemon $nginx -c $NGINX_CONF_FILE
- retval=$?
- echo
- [ $retval -eq 0 ] && touch $lockfile
- return $retval
- }
- stop() {
- echo -n $"Stopping $prog: "
- killproc $prog -TERM
- retval=$?
- echo
- [ $retval -eq 0 ] && rm -f $lockfile
- return $retval
- }
- restart() {
- configtest || return $?
- stop
- sleep 1
- start
- }
- reload() {
- configtest || return $?
- echo -n $"Reloading $prog: "
- killproc $nginx -HUP
- RETVAL=$?
- echo
- }
- force_reload() {
- restart
- }
- configtest() {
- $nginx -t -c $NGINX_CONF_FILE
- }
- rh_status() {
- status $prog
- }
- rh_status_q() {
- rh_status >/dev/null 2>&1
- }
- case "$1" in
- start)
- rh_status_q && exit 0
- $1
- ;;
- stop)
- rh_status_q || exit 0
- $1
- ;;
- restart|configtest)
- $1
- ;;
- reload)
- rh_status_q || exit 7
- $1
- ;;
- force-reload)
- force_reload
- ;;
- status)
- rh_status
- ;;
- condrestart|try-restart)
- rh_status_q || exit 0
- ;;
- *)
- echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
- exit 2
- esac
- 給腳本執行權限
- #chmod a+x /etc/init.d/nginx
- #chkconfig --add nginx
- #chkconfig nginx on
- #service nginx start
二,配置虛擬主機 算法
- #vim /usr/local/nginx/conf/nginx.conf
- .....
- server {
- listen 80; #監聽的端口
- server_name www.andy.com; #訪問此站點的域名
- index index.html index.htm; #默認主頁
- root /www; #網站根目錄
- }
- server {
- ........
- .......
- }
- .......
若是說此服務器的虛擬主機不少,顯然上面的配置方法不易於管理,咱們能夠專門指定一個目錄來放虛擬主機的配置文件,每個虛擬主機單獨使用一個配置文件,這樣的話管理起來也方便多了,配置以下vim
- #vim /usr/local/nginx/conf/nginx.conf
- .....
- .....
- include conf/vhost/*.conf;
- #mkdir /usr/local/nginx/conf/vhost
- #vim /usr/local/nginx/conf/vhost/www.conf
- server {
- listen 80; #監聽的端口
- server_name www.andy.com; #訪問此站點的域名
- index index.html index.htm; #默認主頁
- root /www; #網站根目錄
- }
三,用戶認證
後端
- #這裏使用htpasswd工具所生成的用戶名密碼做爲用戶認證的來源
- #htpasswd -cd /usr/local/nginx/conf/.auth andy
- #htpasswd -d /usr/local/nginx/conf/.auth andy_f
- #vim /usr/local/nginx/conf/vhost/www.conf
- server {
- listen 80;
- server_name www.andy.com;
- index index.html index.htm;
- root /www;
- location / {
- auth_basic "test";
- auth_basic_user_file /usr/local/nginx/conf/.auth;
- }
- #service nginx restart
四, HTTPS 使用自頒發證書實現 瀏覽器
- #創建存放https證書的目錄
- #mkdir -pv /usr/local/nginx/conf/.sslkey
- #生成網站私鑰文件
- #cd /usr/local/nginx/conf/.sslkey
- #openssl genrsa -out https.key 1024
- #生存網站證書文件,須要注意的是在生成的過程當中須要輸入一些信息根據本身的須要輸入,但Common Name 項輸入的必須是訪問網站的FQDN
- #openssl req -new -x509 -key https.key -out https.crt
- #爲了安全起見,將存放證書的目錄權限設置爲400
- #chmod -R 400 /usr/local/nginx/conf/.sslkey
- #vim /usr/local/nginx/conf/vhost/www.conf
- server {
- listen 443;
- server_name www.andy.com;
- index index.html index.htm;
- root /www;
- ssl on;
- ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
- ssl_ciphers AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
- ssl_certificate /usr/local/nginx/conf/.sslkey/https.crt;
- ssl_certificate_key /usr/local/nginx/conf/.sslkey/https.key;
- ssl_session_cache shared:SSL:10m;
- ssl_session_timeout 10m;
- }
- #從新啓動nginx服務
- #service nginx restart
五, 反向代理 帶緩存機制的反向代理緩存
- #vim /usr/local/nginx/conf/nginx/conf
- .......
- .......
- http {
- ......
- proxy_cache_path /var/www/cache levels=1:2 keys_zone=mycache:20m max_size=2048m inactive=60m;
- proxy_temp_path /www/cache/tmp;
- ......
- server {
- listen 80;
- server_name www.andy.com;
- location / {
- proxy_pass http://127.0.0.1/;
- proxy_cache mycache;
- proxy_cache_valid 200 302 60m;
- proxy_cache_valid 404 1m;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- }
- }
- }
- #重啓nginx服務
- #service nginx restart
六, 負載均衡
在nginx中實現負載均衡其實也是使用反向代理的機制,那麼在nginx中默認支持的調度算法有三種,輪詢,加權輪詢,ip_hash, 負載均衡是啥不用解釋了吧,下面來配置下nginx的負載均衡.
- #vim /usr/local/nginx/conf/nginx.conf
- user nginx;
- worker_processes 10;
- error_log logs/error.log crit;
- pid logs/nginx.pid;
- events
- {
- use epoll;
- worker_connections 51000;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- keepalive_timeout 60;
- tcp_nodelay on;
- #指定負載均衡的方式 bbs.andy.com 是一個名字負載均衡集羣的名字
- upstream bbs.andy.com {
- server 172.16.0.2:80; #後端的應用服務器
- server 172.16.0.3:80;
- server 172.16.0.4:80 weight=5; 權重
- server 172.16.0.5:8080 backup; 備份節點,
- ip_hash; #調度算法
- }
- server {
- listen 80;
- server_name bbs.andy.com;
- index index.html index.htm index.php;
- location / {
- proxy_pass http://bbs.andy.com; #指定反代到哪一個主機,或集羣列表中
- #若是後端服務器出現502 或504錯誤代碼的話nginx就不會請求某臺服務器了,當後端服務器又工做正常了,nginx繼續請求,這樣一來達到了後端服務器健康情況檢測的功能,
- proxy_next_upstream http_502 http_504 error timeout invalid_header;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_connect_timeout 600;
- proxy_read_timeout 600;
- proxy_send_timeout 600;
- proxy_buffer_size 8k;
- proxy_temp_file_write_size 64k;
- }
- access_log logs/bbs.log;
- }
- }
七,URL重寫
所謂url重寫就是將某個路徑從新定位到另外一個路徑,跟查找替換差很少,格式以下
語法格式 : rewrite regex replacement [flag];
在nginx中url重寫的處理機制有如下4種,
last 被匹配到的url再進行匹配
break 被匹配到的url就不在進行匹配了
redirect 臨時重定向
permanent 永久重定向
1,好比說訪問某站點的路徑爲/forum/ 此時想使用/bbs來訪問此站點須要作url重寫以下
- location / {
- rewrite ^/forum/?$ /bbs/ permanent;
- }
2,好比說某站點有個圖片服務器(10.0.0.1/p_w_picpaths/ ) 此時訪問某站點上/p_w_picpaths/的資源時但願訪問到圖片服務器上的資源
- location / {
- rewrite ^/p_w_picpaths/(.*\.jpg)$ /p_w_picpaths2/$1 break;
- }
3, 域名跳轉
- server
- {
- listen 80;
- server_name andy.com;
- rewrite ^/ http://www.andy.com/;
- }
4,域名鏡像
- server
- {
- listen 80;
- server_name andy.com;
- rewrite ^/(.*)$ http://www.andy.com/$1 last;
- }
*********************************************************************************************************************************************
在nginx的url重寫中還支持if判斷語句,
語法 if ( 條件 ) { ..... }
應用環境 server ,location
if 判斷的條件以下
一、變量名; false values are: empty string ("", or any string starting with "0";)
二、對於變量進行的比較表達式,可以使用=或!=進行測試;
三、正則表達式的模式匹配:
~ 區分大小的模式匹配
~* 不區分字母大小寫的模式匹配
!~ 和 !~* 分別對上面的兩種測試取反
四、測試文件是否存在-f或!-f
五、測試目錄是否存在-d或!-d
六、測試目錄、文件或連接文件的存在性-e或!-e
七、檢查一個文件的執行權限-x或!-x
*********************************************************************************************************************************************
nginx 的一些內置變量,
http://wiki.nginx.org/HttpCoreModule#Variables 官方文檔
$arg_PARAMETER
$args
$binary_remote_addr
$body_bytes_sent
$content_length
$content_type
$cookie_COOKIE
$document_root
$document_uri
$host
$hostname
$http_HEADER
$sent_http_HEADER
$is_args
$limit_rate
$nginx_version
$query_string
$remote_addr
$remote_port
$remote_user
$request_filename
$request_body
$request_body_file
$request_completion
$request_method
$request_uri
$scheme
$server_addr
$server_name
$server_port
$server_protocol
$uri
*********************************************************************************************************************************************
5,簡單的防盜鏈
- location ~* \.(gif|jpg|png|swf|flv)$ {
- valid_referers none blocked www.andy.com;
- if ($invalid_referer) {
- rewrite ^/ http://www.andy.com/403.html;
- }
6,若是用戶請求的頁面不存在則自定義跳轉
- if (!-f $request_filename) {
- rewrite ^(/.*)$ http://www.andy.com permanent;
- }
7,判斷用戶瀏覽器的類型,做出相應的跳轉,
- if ($http_user_agent ~* MSIE) {
- rewrite ^(.*)$ /msie/$1 break;
- }
OK ,完工了,暫時總結了這麼多