nginx實踐

sgy618發表於2011-07-15
nginx實踐

[@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


location / {

root sina;

index index.html index.htm;

}

}


server {

listen 80;

server_name


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 併發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


#charset koi8-r;


#access_log logs/host.access.log main;


location / {

#root baidu;

#index index.html index.htm;


proxy_pass

??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


#charset koi8-r;


#access_log logs/host.access.log main;


location / {

#root baidu;

#index index.html index.htm;


proxy_pass

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $remote_addr; 可以看到訪問客戶的真實IP

}



3.hash 解決購物車問題

4.根據伺服器新舊

5.。基於urlhash







來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23168012/viewspace-1052518/,如需轉載,請註明出處,否則將追究法律責任。

相關文章