ngnix安装
一、安裝Nginx:
1 : wget下載: http://nginx.org/download/nginx-1.4.2.tar.gz
2 : 進行安裝: tar -zxvf nginx-1.6.2.tar.gz
3 : 下載鎖需要的依賴庫文件:
yum install pcre
yum install pcre-devel
yum install zlib
yum install zlib-devel
4 : 進行configure配置:cd nginx-1.6.2 && ./configure –prefix=/usr/local/nginx 查看是否報錯
5 : 編譯安裝 make && make install
6 : 啟動Nginx:
cd /usr/local/nginx目錄下: 看到如下4個目錄
….conf 配置文件
… html 網頁文件
…logs 日志文件
…sbin 主要二進制程序
啟動命令:/usr/local/nginx/sbin/nginx -s start 關閉(stop)重啟(reload)
成功:查看是否啟動(netstat -ano | grep 80)
失敗:可能為80端口被占用等。
最終:
瀏覽器訪問地址:http://192.168.1.172:80 (看到歡迎頁面即可)
二、使用Nginx:簡單與單臺Tomcat整合
a) 首先找到nginx.conf文件:vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost:80;
location / {
proxy_pass http://localhost:8080;
}
//…others
}
三、詳細使用(nginx就是去配置其文件而已),如下所示:
1. #開啟進程數(shù) <=CPU數(shù)
2. worker_processes 1;
3.
4. #錯誤日志保存位置
5. #error_log logs/error.log;
6. #error_log logs/error.log notice;
7. #error_log logs/error.log info;
8.
9. #進程號保存文件
10. #pid logs/nginx.pid;
11.
12. #等待事件
13. events {
14. #每個進程最大連接數(shù)(最大連接=連接數(shù)x進程數(shù))
15. #每個worker允許同時產生多少個鏈接,默認1024
16. worker_connections 1024;
17. }
18.
19.
20. http {
21. #文件擴展名與文件類型映射表
22. include mime.types;
23. #默認文件類型
24. default_type application/octet-stream;
25. #日志文件輸出格式 這個位置相于全局設置
26. #log_format main ‘remoteaddr?remoteaddr?remote_user [timelocal]"timelocal]"request” ’
27. # ‘statusstatusbody_bytes_sent “http_referer”?’??
28.?#?‘”http_referer”?’??28.?#?‘”http_user_agent" "$http_x_forwarded_for”’;
29. #請求日志保存位置
30. #access_log logs/access.log main;
31. #打開發(fā)送文件
32. sendfile on;
33. #tcp_nopush on;
34. #連接超時時間
35. #keepalive_timeout 0;
36. keepalive_timeout 65;
37. #打開gzip壓縮
38. #gzip on;
39. #設定請求緩沖
40. client_header_buffer_size 1k;
41. large_client_header_buffers 4 4k;
42. #設定負載均衡的服務器列表
43. upstream myproject {
44. #weigth參數(shù)表示權值,權值越高被分配到的幾率越大
45. #max_fails 當有#max_fails個請求失敗,就表示后端的服務器不可用,默認為1,將其設置為0可以關閉檢查
46. #fail_timeout 在以后的#fail_timeout時間內nginx不會再把請求發(fā)往已檢查出標記為不可用的服務器
47. #這里指定多個源服務器,ip:端口,80端口的話可寫可不寫
48. server 192.168.1.78:8080 weight=5 max_fails=2 fail_timeout=600s;
49. #server 192.168.1.222:8080 weight=3 max_fails=2 fail_timeout=600s;
50. }
51.
52. #第一個虛擬主機
53. server {
54. #監(jiān)聽IP端口
55. listen 80;
56. #主機名
57. server_name localhost;
58. #設置字符集
59. #charset koi8-r;
60. #本虛擬server的訪問日志 相當于局部變量
61. #access_log logs/host.access.log main;
62. #對本server”/”啟用負載均衡
63. location / {
64. #root /root; #定義服務器的默認網站根目錄位置
65. #index index.php index.html index.htm; #定義首頁索引文件的名稱
66. proxy_pass http://myproject; #請求轉向myproject定義的服務器列表
67. #以下是一些反向代理的配置可刪除.
68. # proxy_redirect off;
69. # proxy_set_header Host host;??
70.?#?proxy_set_header?X-Real-IPhost;??70.?#?proxy_set_header?X-Real-IPremote_addr;
71. # proxy_set_header X-Forwarded-For proxy_add_x_forwarded_for;??
72.?#?client_max_body_size?10m;?#允許客戶端請求的最大單文件字節(jié)數(shù)??
73.?#?client_body_buffer_size?128k;?#緩沖區(qū)代理緩沖用戶端請求的最大字節(jié)數(shù),??
74.?#?proxy_connect_timeout?90;?#nginx跟后端服務器連接超時時間(代理連接超時)??
75.?#?proxy_send_timeout?90;?#后端服務器數(shù)據(jù)回傳時間(代理發(fā)送超時)??
76.?#?proxy_read_timeout?90;?#連接成功后,后端服務器響應時間(代理接收超時)??
77.?#?proxy_buffer_size?4k;?#設置代理服務器(nginx)保存用戶頭信息的緩沖區(qū)大小??
78.?#?proxy_buffers?4?32k;?#proxy_buffers緩沖區(qū),網頁平均在32k以下的話,這樣設置??
79.?#?proxy_busy_buffers_size?64k;?#高負荷下緩沖大小(proxy_buffers*2)??
80.?#?proxy_temp_file_write_size?64k;?#設定緩存文件夾大小,大于這個值,將從upstream服務器傳??
81.?}??
82.?location?/upload?{??
83.?alias?e:/upload;??
84.?}??
85.?#設定查看Nginx狀態(tài)的地址??
86.?location?/NginxStatus?{??
87.?stub_status?on;??
88.?access_log?off;??
89.?#allow?192.168.0.3;??
90.?#deny?all;??
91.?#auth_basic?“NginxStatus”;??
92.?#auth_basic_user_file?conf/htpasswd;??
93.?}??
94.?#error_page?404?/404.html;??
95.?#?redirect?server?error?pages?to?the?static?page?/50x.html??
96.?#?定義錯誤提示頁面??
97.?error_page?500?502?503?504?/50x.html;??
98.?location?=?/50x.html?{??
99.?root?html;??
100.????}??
101.????#?proxy?the?PHP?scripts?to?Apache?listening?on?127.0.0.1:80??
102.????#??
103.????#location?~?.phpproxy_add_x_forwarded_for;??72.?#?client_max_body_size?10m;?#允許客戶端請求的最大單文件字節(jié)數(shù)??73.?#?client_body_buffer_size?128k;?#緩沖區(qū)代理緩沖用戶端請求的最大字節(jié)數(shù),??74.?#?proxy_connect_timeout?90;?#nginx跟后端服務器連接超時時間(代理連接超時)??75.?#?proxy_send_timeout?90;?#后端服務器數(shù)據(jù)回傳時間(代理發(fā)送超時)??76.?#?proxy_read_timeout?90;?#連接成功后,后端服務器響應時間(代理接收超時)??77.?#?proxy_buffer_size?4k;?#設置代理服務器(nginx)保存用戶頭信息的緩沖區(qū)大小??78.?#?proxy_buffers?4?32k;?#proxy_buffers緩沖區(qū),網頁平均在32k以下的話,這樣設置??79.?#?proxy_busy_buffers_size?64k;?#高負荷下緩沖大小(proxy_buffers*2)??80.?#?proxy_temp_file_write_size?64k;?#設定緩存文件夾大小,大于這個值,將從upstream服務器傳??81.?}??82.?location?/upload?{??83.?alias?e:/upload;??84.?}??85.?#設定查看Nginx狀態(tài)的地址??86.?location?/NginxStatus?{??87.?stub_status?on;??88.?access_log?off;??89.?#allow?192.168.0.3;??90.?#deny?all;??91.?#auth_basic?“NginxStatus”;??92.?#auth_basic_user_file?conf/htpasswd;??93.?}??94.?#error_page?404?/404.html;??95.?#?redirect?server?error?pages?to?the?static?page?/50x.html??96.?#?定義錯誤提示頁面??97.?error_page?500?502?503?504?/50x.html;??98.?location?=?/50x.html?{??99.?root?html;??100.????}??101.????#?proxy?the?PHP?scripts?to?Apache?listening?on?127.0.0.1:80??102.????#??103.????#location?~?.php {
104. # proxy_pass http://127.0.0.1;
105. #}
106. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
107. #
108. #location ~ .php{??
109.????#?root?html;??
110.????#?fastcgi_pass?127.0.0.1:9000;??
111.????#?fastcgi_index?index.php;??
112.????#?fastcgi_param?SCRIPT_FILENAME?/scripts{??109.????#?root?html;??110.????#?fastcgi_pass?127.0.0.1:9000;??111.????#?fastcgi_index?index.php;??112.????#?fastcgi_param?SCRIPT_FILENAME?/scriptsfastcgi_script_name;
113. # include fastcgi_params;
114. #}
115. # deny access to .htaccess files, if Apache’s document root
116. # concurs with nginx’s one
117. #
118. #location ~ /.ht {
119. # deny all;
120. #}
121. }
122.
123.
124. # another virtual host using mix of IP-, name-, and port-based configuration
125. #
126. #server {
127. #多監(jiān)聽
128. # listen 8000;
129. #主機名
130. # listen somename:8080;
131. # server_name somename alias another.alias;
132.
133. # location / {
134. #WEB文件路徑
135. # root html;
136. #默認首頁
137. # index index.html index.htm;
138. # }
139. #}
140.
141.
142. # HTTPS server HTTPS SSL加密服務器
143. #
144. #server {
145. # listen 443;
146. # server_name localhost;
147.
148. # ssl on;
149. # ssl_certificate cert.pem;
150. # ssl_certificate_key cert.key;
151.
152. # ssl_session_timeout 5m;
153.
154. # ssl_protocols SSLv2 SSLv3 TLSv1;
155. # ssl_ciphers HIGH:!aNULL:!MD5;
156. # ssl_prefer_server_ciphers on;
157.
158. # location / {
159. # root html;
160. # index index.html index.htm;
161. # }
162. #}
163. }
四.配置tomcat集群負載均衡(這里我們之間配置2個tomcat到nginx里,然后進行測試)
五.其他配置信息文件說明
參考博客1:http://blog.csdn.net/wave_1102/article/details/44475093
參考博客2:http://blog.csdn.net/shimiso/article/details/8690897
總結
- 上一篇: 大数据学习路线图
- 下一篇: FastDFS学习笔记