0.jdk安裝配置php
#下載相應的jdk軟件包,而後解壓安裝,我這裏包名稱爲:jdk-7u25-linux-x64.tar.gz tar -xzf jdk-7u25-linux-x64.tar.gz
mkdir -p /usr/java/
mv jdk1.7.0_25/ /usr/java/ #而後配置環境變量,這樣能夠任何地方引用jdk,以下配置: #vi /etc/profile 最後面加入如下語句: export JAVA_HOME=/usr/java/jdk1.7.0_25 export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/bin #source /etc/profile #使環境變量立刻生效 #java --version #查看java版本,看到jdk1.7.0_25版本即表明java jdk安裝成功。
1.官網css
http://nginx.org/html
2.nginx安裝java
2.1cd /user/local/software目錄下(沒有的話,就建立)linux
2.2上傳nginx包及高可用包到該目錄下nginx
nginx-1.6.2.tar.gz
keepalived-1.2.18.tar.gz
2.3解壓至 /usr/local下c++
tar -zxf nginx-1.6.2.tar.gz /usr/local
2.4下載鎖須要的依賴庫文件:web
yum install pcre yum install pcre-devel yum install zlib yum install zlib-devel
2.5進行configure配置:正則表達式
cd nginx-1.6.2 && ./configure --prefix=/usr/local/nginx
下圖表明成功apache
查看是否報錯:
報錯1:error: C compiler cc is not found
解決方案:
安裝gcc-c++包
命令:yum -y install gcc-c++
-y:全部的詢問均選擇yes
2.6編譯安裝(cd 到 /usr/local/nginx-1.6.2目錄下)
make && make install
2.7啓動nginx
cd /usr/local/nginx目錄下: 看到以下4個目錄
....conf 配置文件
... html 網頁文件
...logs 日誌文件
...sbin 主要二進制程序
啓動命令:(/usr/local/nginx/sbin/nginx -s start)
/usr/local/nginx/sbin/nginx
查看是否啓動成功:(netstat -ano | grep 80)或者以下:
ps -ef|grep nginx
關閉命令:(/usr/local/nginx/sbin/nginx -s stop)
/usr/local/nginx/sbin/nginx -s stop
重啓命令:(/usr/local/nginx/sbin/nginx -s reload)
/usr/local/nginx/sbin/nginx -s reload
瀏覽器訪問地址:(看到歡迎頁面便可)
如果失敗:可能爲80端口被佔用等。
lsof -i:80查看進程pid,而後kill -9 pid便可
3.nginx虛擬主機配置(即配置nginx.conf文件)
3.1假設將nginx.conf中的80端口改成8081
cd 到/etc/sysconfig/下,找到iptables文件,添加以下一行,端口放行記錄
#user nobody; #開啓進程數 <=CPU數 worker_processes 1; #錯誤日誌保存位置 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #進程號保存文件 #pid logs/nginx.pid; #每一個進程最大鏈接數(最大鏈接=鏈接數x進程數)每一個worker容許同時產生多少個連接,默認1024 events { worker_connections 1024; } http { #文件擴展名與文件類型映射表 include mime.types; #默認文件類型 default_type application/octet-stream; #日誌文件輸出格式 這個位置相於全局設置 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #請求日誌保存位置(這裏是全局的配置,適用於全部server) #access_log logs/access.log main; #打開發送文件 sendfile on; #tcp_nopush on; #keepalive_timeout 0; #鏈接超時時間 keepalive_timeout 65; #打開gzip壓縮 #gzip on; #設定請求緩衝 #client_header_buffer_size 1k; #large_client_header_buffers 4 4k; #設定負載均衡的服務器列表 #upstream myproject { #weigth參數表示權值,權值越高被分配到的概率越大 #max_fails 當有#max_fails個請求失敗,就表示後端的服務器不可用,默認爲1,將其設置爲0能夠關閉檢查 #fail_timeout 在之後的#fail_timeout時間內nginx不會再把請求發往已檢查出標記爲不可用的服務器 #} #webapp #upstream myapp { # server 192.168.1.171:8080 weight=1 max_fails=2 fail_timeout=30s; # server 192.168.1.172:8080 weight=1 max_fails=2 fail_timeout=30s; #} #配置虛擬主機,基於域名、ip和端口 server { #監聽端口 listen 80; #監聽域名 server_name localhost; #charset koi8-r; #nginx訪問日誌放在logs/host.access.log下,而且使用main格式(還能夠自定義格式) #access_log logs/host.access.log main; #返回的相應文件地址 location / { #設置客戶端真實ip地址 #proxy_set_header X-real-ip $remote_addr; #負載均衡反向代理 #proxy_pass http://myapp; #返回根路徑地址(相對路徑:相對於/usr/local/nginx/) root html; #默認訪問文件 index index.html index.htm; } #配置反向代理tomcat服務器:攔截.jsp結尾的請求轉向到tomcat #location ~ \.jsp$ { # proxy_pass http://192.168.1.171:8080;#此處是tomcat所在服務器的ip:端口 #} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # #錯誤頁面及其返回地址 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } #虛擬主機配置: server { listen 1234; server_name bhz.com; location / { #正則表達式匹配uri方式:在/usr/local/nginx/bhz.com下 創建一個test123.html 而後使用正則匹配 #location ~ test { ## 重寫語法:if return (條件 = ~ ~*) #if ($remote_addr = 192.168.1.200) { # return 401; #} #if ($http_user_agent ~* firefox) { # rewrite ^.*$ /firefox.html; # break; #} root bhz.com; index index.html; } #location /goods { # rewrite "goods-(\d{1,5})\.html" /goods-ctrl.html; # root bhz.com; # index index.html; #} #配置訪問日誌(這裏是局部的配置,適用於該server) access_log logs/bhz.com.access.log main; } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
3.3location攔截說明:
3.3.1將指定IP加入黑名單,告知其鑑權失敗(注意空格!!!!!!!!!!!!!)
192.168.0.103訪問時,以下結果
3.3.2 攔截user_agent含有firefox的請求
3.3.3實現動靜分離
3.3.3.1若是是請求的是某個url,靜態網頁,則走其中一個location
3.3.3.2若是是訪問的是另外一個url,動態網頁,如jsp,php,則能夠經過反向代理,轉發給apache或者tomcat,走另外一個location
基礎語法:
location = {}#精準匹配
location {}#通常匹配
location ~{}#正則匹配
1 location [=|~|~*|^~] /uri/ { … } 2 符號 含義 3 = 開頭表示精確匹配 4 ^~ 開頭表示uri以某個常規字符串開頭,理解爲匹配 url路徑便可。nginx不對url作編碼,所以請求爲/static/20%/aa,能夠被規則^~ /static/ /aa匹配到(注意是空格) 5 ~ 開頭表示區分大小寫的正則匹配 6 ~* 開頭表示不區分大小寫的正則匹配 7 !~和!~* 分別爲區分大小寫不匹配及不區分大小寫不匹配 的正則 8 / 通用匹配,任何請求都會匹配到。 9 多個location配置的狀況下匹配順序爲(參考資料而來,還未實際驗證,試試就知道了,沒必要拘泥,僅供參考): 10 首先匹配 = 11 其次匹配 ^~ 12 其次是按文件中順序的正則匹配 13 最後是交給 / 通用匹配 14 當有匹配成功時候,中止匹配,按當前匹配規則處理請求 15 例子,有以下匹配規則: 16 location = / { 17 #規則A 18 } 19 location = /login { 20 #規則B 21 } 22 location ^~ /static/ { 23 #規則C 24 } 25 location ~ \.(gif|jpg|png|js|css)$ { 26 #規則D 27 } 28 location ~* \.png$ { 29 #規則E 30 } 31 location !~ \.xhtml$ { 32 #規則F 33 } 34 location !~* \.xhtml$ { 35 #規則G 36 } 37 location / { 38 #規則H 39 } 40 那麼產生的效果以下: 41 訪問根目錄/, 好比http://localhost/ 將匹配規則A 42 訪問 http://localhost/login 將匹配規則B,http://localhost/register 則匹配規則H 43 訪問 http://localhost/static/a.html 將匹配規則C 44 訪問 http://localhost/a.gif, http://localhost/b.jpg 將匹配規則D和規則E,可是規則D順序優先,規則E不起做用,而 http://localhost/static/c.png 則優先匹配到規則C 45 訪問 http://localhost/a.PNG 則匹配規則E,而不會匹配規則D,由於規則E不區分大小寫。 46 訪問 http://localhost/a.xhtml 不會匹配規則F和規則G,http://localhost/a.XHTML不會匹配規則G,由於不區分大小寫。規則F,規則G屬於排除法,符合匹配規則可是不會匹配到,因此想一想看實際應用中哪裏會用到。 47 訪問 http://localhost/category/id/1111 則最終匹配到規則H,由於以上規則都不匹配,這個時候應該是nginx轉發請求給後端應用服務器,好比FastCGI(php),tomcat(jsp),nginx做爲方向代理服務器存在。 48 因此實際使用中,我的以爲至少有三個匹配規則定義,以下: 49 #直接匹配網站根,經過域名訪問網站首頁比較頻繁,使用這個會加速處理,官網如是說。 50 #這裏是直接轉發給後端應用服務器了,也能夠是一個靜態首頁 51 # 第一個必選規則 52 location = / { 53 proxy_pass http://tomcat:8080/index 54 } 55 # 第二個必選規則是處理靜態文件請求,這是nginx做爲http服務器的強項 56 # 有兩種配置模式,目錄匹配或後綴匹配,任選其一或搭配使用 57 location ^~ /static/ { 58 root /webroot/static/; 59 } 60 location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { 61 root /webroot/res/; 62 } 63 #第三個規則就是通用規則,用來轉發動態請求到後端應用服務器 64 #非靜態文件請求就默認是動態請求,本身根據實際把握 65 #畢竟目前的一些框架的流行,帶.php,.jsp後綴的狀況不多了 66 location / { 67 proxy_pass http://tomcat:8080/ 68 } 69 ReWrite語法 70 71 last – 基本上都用這個Flag 72 break – 停止Rewirte,不在繼續匹配 73 redirect – 返回臨時重定向的HTTP狀態302 74 permanent – 返回永久重定向的HTTP狀態301 75 1、下面是能夠用來判斷的表達式: 76 -f和!-f用來判斷是否存在文件 77 -d和!-d用來判斷是否存在目錄 78 -e和!-e用來判斷是否存在文件或目錄 79 -x和!-x用來判斷文件是否可執行 80 2、下面是能夠用做判斷的全局變量 81 例:http://localhost:88/test1/test2/test.php 82 $host:localhost 83 $server_port:88 84 $request_uri:http://localhost:88/test1/test2/test.php 85 $document_uri:/test1/test2/test.php 86 $document_root:D:\nginx/html 87 $request_filename:D:\nginx/html/test1/test2/test.php 88 Redirect語法 89 90 server { 91 listen 80; 92 server_name start.igrow.cn; 93 index index.html index.php; 94 root html; 95 if ($http_host !~ 「^star\.igrow\.cn$" { 96 rewrite ^(.*) http://star.igrow.cn$1 redirect; 97 } 98 } 99 防盜鏈 100 101 location ~* \.(gif|jpg|swf)$ { 102 valid_referers none blocked start.igrow.cn sta.igrow.cn; 103 if ($invalid_referer) { 104 rewrite ^/ http://$host/logo.png; 105 } 106 } 107 根據文件類型設置過時時間 108 109 location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ { 110 if (-f $request_filename) { 111 expires 1h; 112 break; 113 } 114 } 115 禁止訪問某個目錄 116 117 location ~* \.(txt|doc)${ 118 root /data/www/wwwroot/linuxtone/test; 119 deny all; 120 }
4.關鍵模塊配置
4.1靜態頁面攔截規則 location(在server裏)
4.2反向代理 proxy_pass(在location裏)
4.3負載均衡 upstream(在server外)