当前位置:   article > 正文

Nginx基础详解

Nginx基础详解

nginx introduction

  1. NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.
  2. NGINX is one of a handful of servers written to address the C10K problem. Unlike traditional servers, NGINX doesn’t rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but more importantly, predictable amounts of memory under load. Even if you don’t expect to handle thousands of simultaneous requests, you can still benefit from NGINX’s high-performance and small memory footprint. NGINX scales in all directions: from the smallest VPS all the way up to large clusters of servers.
  3. NGINX powers several high-visibility sites, such as Netflix, Hulu, Pinterest, CloudFlare, Airbnb, WordPress.com, GitHub, SoundCloud, Zynga, Eventbrite, Zappos, Media Temple, Heroku, RightScale, Engine Yard, MaxCDN and many others.

master功能:一个master进程负责加载和分析配置文件、管理worker进程、平滑升级。
worker功能:1.响应客户端多个请求
                      2.基于http协议反向代理后端web,
                      3.基于fastCGI把用户动态请求反向代理给application server,实现动态资源的解析,
                      4.基于memcache协议把用户的请求交给后端的KV缓存服务器
                      5.每个worker内部都有各种模块worker通过kevent epoll/select机制响应给客户端的,
proxy cache功能:nginx作为代理服务器时,客户端请求的连接支持代理缓冲功能。缓存相关的进程:
                        cache loader:载入缓存对象
                        cache manager:管理缓存对象
支持mmap:数据由磁盘直接以页面形式映射进行内存中;

一、详细描述常见nginx常用模块和模块的使用示例   

实验环境:

nginx服务器:192.168.170.8

客户端:192.168.170.9     

  1. yum安装nginx
  2. [root@node1 ~]# yum -y install nginx
  3. 查看nginx生成的配置文件
  4. [root@node1 ~]# rpm -ql nginx
  5. 配置文件的组成部分:
  6. 主配置文件:nginx.conf
  7. include conf.d/*.conf nginx.conf配置文件包含conf.d/*.conf所有文件
  8. fastcgi, uwsgi,scgi等协议相关的配置文件
  9. mime.types:支持的mime类型
  10. 主程序文件:/usr/sbin/nginx
  11. Unit File:nginx.service
  12. 创建nginx目录,并在目录下创建nginx测试页
  13. [root@node1 ~]# cd /etc/nginx/conf.d/
  14. [root@node1 conf.d]#mkdir /data/nginx/vhost -pv
  15. [root@node1 conf.d]#vi /data/nginx/vhost/index.html
  16. <h1>vhost</h1>
  17. 编辑nginx配置文件
  18. [root@node1 conf.d]#vi /vhost.conf
  19. server {
  20. listen 80;
  21. server_name www.node1.com;
  22. root /data/nginx/vhost; #根目录指向所创建的nginx目录
  23. }
  24. [root@node1 conf.d]#nginx -t 开启nginx服务功能
  25. [root@node1 conf.d]#nginx -s reload 重新加载nginx服务
  26. 客户端测试
  27. [root@node2 ~]# curl http://192.168.170.8
  28. <h1>vhost</h1>
  29. 注意:测试时建议注释nginx.conf下server段的所有配置项,避免测试无法进行。
  30. 示例2
  31. [root@node1 conf.d]# vi vhost.conf
  32. server {
  33. listen 80;
  34. server_name www.node1.com;
  35. root /data/nginx/vhost;
  36. location / {
  37. deny 192.168.170.9;
  38. allow all;
  39. }
  40. }
  41. [root@node1 conf.d]#nginx -t 开启nginx服务功能
  42. [root@node1 conf.d]#nginx -s reload 重新加载nginx服务
  43. 客户端node2测试禁止访问
  44. [root@node2 ~]# curl http://192.168.170.8
  45. <html>
  46. <head><title>403 Forbidden</title></head>
  47. <body bgcolor="white">
  48. <center><h1>403 Forbidden</h1></center>
  49. <hr><center>nginx/1.12.2</center>
  50. </body>
  51. </html>
  52. [root@node2 ~]#
  53. 其它客户端访问正常
  54. [root@node3 ~]# curl http://192.168.170.8
  55. <h1>vhost</h1>
  56. [root@node3 ~]#
  57. 示例3
  58. [root@node1 conf.d]# cat /data/nginx/vhost/
  59. admin/ blue.jpg images/ nginx.html sea.jpg
  60. autumn.jpg forest.jpg index.html nightfall.jpg
  61. [root@node1 conf.d]# cat /data/nginx/vhost/
  62. [root@node1 conf.d]# vi vhost.conf
  63. server {
  64. listen 80;
  65. server_name www.node1.com;
  66. root /data/nginx/vhost;
  67. location ~*\.(jpg|png)$ { 对URI做正则表达式模式匹配,不区分字符大小写;匹配jpg或png结尾的所有文件类型
  68. deny 192.168.170.9;
  69. allow all;
  70. }
  71. }
  72. [root@node1 conf.d]#nginx -t 开启nginx服务功能
  73. [root@node1 conf.d]#nginx -s reload 重新加载nginx服务
  74. node2测试正常
  75. [root@node2 ~]# curl http://192.168.170.8
  76. <h1>vhost</h1>
  77. 浏览器输入http://192.168.170.8/autumn.jpg
  78. 正常
  79. 示例4
  80. [root@node1 conf.d]# vi vhost.conf
  81. server {
  82. listen 80;
  83. server_name www.node1.com;
  84. root /data/nginx/vhost;
  85. location / {
  86. #root /data/nignx/vhost2;
  87. allow all;
  88. }
  89. location ~*\.(jpg|png)$ {
  90. deny 192.168.170.9;
  91. allow all;
  92. }
  93. location ^~ /images/ { 对URI的左半部分做匹配检查,不区分字符大小写;
  94. root /data/pictures/;
  95. }
  96. }
  97. 创建images目录,并移动.jpg图片到该目录下提供测试页
  98. [root@node1 conf.d]# mkdir /data/nginx/vhost/images
  99. [root@node1 conf.d]# mv /data/nginx/vhost/green.jpg /data/nginx/vhost/images
  100. [root@node1 conf.d]# cd /data/nginx/vhost/images
  101. [root@node1 images]# ls
  102. green.jpg
  103. [root@node1 conf.d]# nginx -t
  104. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  105. nginx: configuration file /etc/nginx/nginx.conf test is successful
  106. [root@node1 conf.d]]# nginx -s reload
  107. 浏览器测试,无法访问到。
  108. [root@node1 conf.d]# curl http://192.168.170.8/images/green.ipg
  109. <html>
  110. <head><title>404 Not Found</title></head>
  111. <body bgcolor="white">
  112. <center><h1>404 Not Found</h1></center>
  113. <hr><center>nginx/1.12.2</center>
  114. </body>
  115. </html>
  116. [root@node1 conf.d]#
  117. 创建data/pictures/images目录,上传图片到该目录作为测试页。
  118. [root@node1 conf.d]# mkdir /data/pictures/images
  119. [root@node1 conf.d]# cd /data/pictures/images
  120. [root@node1 images]# ls
  121. Cloud.jpg peach blossom.jpg
  122. 浏览器http://192.168.170.8/images/Cloud.jpg
  123. 正常
  124. 说明:
  125. location中使用root指令和alias指令的意义不同;
  126. root,给定的路径对应于location中的/uri/左侧的/
  127. [root@node1 conf.d]# cd /data/pictures/
  128. [root@node1 pictures]# ls
  129. images landscape.jpg
  130. [root@node1 conf.d]# vi vhost.conf
  131. server {
  132. listen 80;
  133. server_name www.node1.com;
  134. root /data/nginx/vhost;
  135. location / {
  136. #root /data/nignx/vhost2;
  137. allow all;
  138. }
  139. location ~*\.(jpg|png)$ {
  140. deny 192.168.170.9;
  141. allow all;
  142. }
  143. #location ^~ /images/ {
  144. # root /data/pictures/;
  145. #}
  146. location ^~ /images/ {
  147. alias /data/pictures/;
  148. }
  149. }
  150. [root@node1 conf.d]# nginx -s reload
  151. 浏览器输入http://192.168.170.8/images/landscape.jpg
  152. 正常
  153. 示例5
  154. 自定义错误页面
  155. [root@node1 conf.d]# vi vhost.conf
  156. server {
  157. listen 80;
  158. server_name www.node1.com;
  159. root /data/nginx/vhost;
  160. location / {
  161. #root /data/nignx/vhost2;
  162. allow all;
  163. }
  164. location ~*\.(jpg|png)$ {
  165. deny 192.168.170.9;
  166. allow all;
  167. }
  168. #location ^~ /images/ {
  169. # root /data/pictures/;
  170. #}
  171. location ^~ /images/ {
  172. alias /data/pictures/;
  173. }
  174. error_page 404 =202 /notfound.html; 指定404响应码转成202返回给客户端
  175. location = /notfound.html { 对URI做精确匹配/notfound.html
  176. root /data/nginx/error_pages; 指定root目录
  177. }
  178. }
  179. 编辑错误提示信息
  180. [root@node1 conf.d]# vi /data/nginx/error_pages/notfound.html
  181. <h1>error page</h1>
  182. [root@node1 conf.d]# nginx -s reload
  183. [root@node1 conf.d]#
  184. 客户端测试访问正常
  185. [root@node2 ~]# curl http://192.168.170.8/test.html
  186. <h1>error page</h1>
  187. [root@node2 ~]#
  188. 示例6
  189. 基于basic认证的nginx服务
  190. [root@node1 ~]# yum -y install httpd-tools
  191. 密码生成工具是通过https-tools来实现,所以必须先安装好。
  192. [root@node1 ~]# htpasswd -c -m /etc/nginx/.ngxpasswd tom
  193. New password:
  194. Re-type new password:
  195. Adding password for user tom
  196. [root@node1 ~]# htpasswd -m /etc/nginx/.ngxpasswd jerry
  197. New password:
  198. Re-type new password:
  199. Adding password for user jerry
  200. [root@node1 ~]# cat /etc/nginx/.ngxpasswd
  201. tom:$apr1$PYy15HQl$DZGQU0ATbeDsVOsaHK6sC/
  202. jerry:$apr1$oyQFem.h$mw0PyHSb9.H46khELlc9O1
  203. [root@node1 ~]# cd /etc/nginx/conf.d/
  204. [root@node1 conf.d]# vi vhost.conf
  205. server {
  206. listen 80;
  207. server_name www.node1.com;
  208. root /data/nginx/vhost;
  209. location / {
  210. #root /data/nignx/vhost2;
  211. allow all;
  212. }
  213. location ~*\.(jpg|png)$ {
  214. deny 192.168.170.9;
  215. allow all;
  216. }
  217. location ~* ^/(admin|login) { 配置url路径 对URI的左半部分做匹配检查,匹配admin或login目录所在的测试页
  218. auth_basic "admin area"; 指定认证提示符
  219. auth_basic_user_file /etc/nginx/.ngxpasswd; 指定认证用户和密码所在文件
  220. }
  221. #location ^~ /images/ {
  222. # root /data/pictures/;
  223. #}
  224. location ^~ /images/ {
  225. alias /data/pictures/;
  226. }
  227. error_page 404 =202 /notfound.html;
  228. location = /notfound.html {
  229. root /data/nginx/error_pages;
  230. }
  231. }
  232. [root@node1 conf.d]# mkdir /data/nginx/vhost/admin
  233. 创建admin目录下用户认证登录成功后的测试页
  234. [root@node1 conf.d]# vi /data/nginx/vhost/admin/index.html
  235. <h1>Admin Area</h1>
  236. [root@node1 conf.d]#
  237. [root@node1 conf.d]# nginx -t
  238. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  239. nginx: configuration file /etc/nginx/nginx.conf test is successful
  240. [root@node1 conf.d]# nginx -s reload
  241. 浏览器输入http://192.168.170.8/admin/ 输入用户名:admin 密码:123456 登录成功
  242. 正常
  243. 示例7
  244. nginx状态信息页
  245. [root@node1 conf.d]# vi vhost.conf
  246. server {
  247. listen 80;
  248. server_name www.node1.com;
  249. root /data/nginx/vhost;
  250. location / {
  251. #root /data/nignx/vhost2;
  252. allow all;
  253. }
  254. location ~*\.(jpg|png)$ {
  255. deny 192.168.170.9;
  256. allow all;
  257. }
  258. location ~* ^/(admin|login) {
  259. auth_basic "admin area";
  260. auth_basic_user_file /etc/nginx/.ngxpasswd;
  261. }
  262. #location ^~ /images/ {
  263. # root /data/pictures/;
  264. #}
  265. location ^~ /images/ {
  266. alias /data/pictures/;
  267. }
  268. error_page 404 =202 /notfound.html;
  269. location = /notfound.html {
  270. root /data/nginx/error_pages;
  271. }
  272. location /ngxstatus {
  273. stub_status;
  274. }
  275. }
  276. [root@node1 conf.d]# nginx -t
  277. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  278. nginx: configuration file /etc/nginx/nginx.conf test is successful
  279. [root@node1 conf.d]# nginx -s reload
  280. 浏览器输入http://192.168.170.8/ngxstatus
  281. Active connections: 2
  282. server accepts handled requests
  283. 57 57 45
  284. Reading: 0 Writing: 1 Waiting: 1
  285. 各个状态参数说明:
  286. Active connections: 活动状态的连接数;
  287. accepts:已经接受的客户端请求的总数;
  288. handled:已经处理完成的客户端请求的总数;
  289. requests:客户端发来的总的请求数;
  290. Reading:处于读取客户端请求报文首部的连接的连接数;
  291. Writing:处于向客户端发送响应报文过程中的连接数;
  292. Waiting:处于等待客户端发出请求的空闲连接数;
  293. 示例8
  294. 访问日志信息
  295. [root@node1 conf.d]# vi vhost.conf
  296. server {
  297. listen 80;
  298. server_name www.node1.com;
  299. root /data/nginx/vhost;
  300. access_log /var/log/nginx/vhost_access.log main;
  301. location / {
  302. #root /data/nignx/vhost2;
  303. allow all;
  304. }
  305. location ~*\.(jpg|png)$ {
  306. deny 192.168.170.9;
  307. allow all;
  308. }
  309. location ~* ^/(admin|login) {
  310. auth_basic "admin area";
  311. auth_basic_user_file /etc/nginx/.ngxpasswd;
  312. }
  313. #location ^~ /images/ {
  314. # root /data/pictures/;
  315. #}
  316. location ^~ /images/ {
  317. alias /data/pictures/;
  318. }
  319. error_page 404 =202 /notfound.html;
  320. location = /notfound.html {
  321. root /data/nginx/error_pages;
  322. }
  323. location /ngxstatus {
  324. stub_status;
  325. }
  326. }
  327. [root@node1 conf.d]# nginx -t
  328. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  329. nginx: configuration file /etc/nginx/nginx.conf test is successful
  330. [root@node1 conf.d]# nginx -s reload
  331. 查看访问信息记录日志
  332. [root@node1 conf.d]# tail /var/log/nginx/
  333. access.log error.log vhost_access.log
  334. [root@node1 conf.d]# tail /var/log/nginx/vhost_access.log
  335. 172.17.1.29 - - [10/Nov/2018:16:03:41 +0800] "GET /images/Cloud.jpg HTTP/1.1" 202 20 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36" "-"
  336. 172.17.1.29 - tom [10/Nov/2018:16:03:47 +0800] "GET /admin/ HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36" "-"
  337. [root@node1 conf.d]#
  338. 示例9
  339. 压缩功能
  340. [root@node1 nginx]# vi nginx.conf
  341. http {
  342. gzip on;
  343. gzip_comp_level 6;
  344. gzip_min_length 64;
  345. gzip_proxied any;
  346. gzip_types text/xml text/css application/javascript;
  347. }
  348. [root@node1 nginx]# nginx -t
  349. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  350. nginx: configuration file /etc/nginx/nginx.conf test is successful
  351. [root@node1 nginx]# nginx -s reload
  352. [root@node1 nginx]# cp nginx.conf /data/nginx/vhost/nginx.html
  353. 浏览器输入http://192.168.170.8/nginx.html F12看到确实是通过gzip压缩的文本
  354. 正常
  355. cp /etc/nginx/conf.d/vhost.conf /etc/nginx/conf.d/vhost.conf.bak
  356. vi /etc/nginx/conf.d/vhost.conf
  357. ssl on;
  358. ssl_certificate /etc/nginx/ssl/nginx.crt;
  359. ssl_certificate_key /etc/nginx/ssl/nginx.key;
  360. ssl_protocols sslv3 tlsv1 tlsv1.1 tlsv1.2;
  361. ssl_session_cache shared:SSL:10m;
  362. node2
  363. 基于ssl安全的网页
  364. node2 192.168.170.9 作为发证者
  365. node1 192.168.170.8 作为
  366. [root@node2 ~]#cd /etc/pki/CA/
  367. [root@node2 CA]# ls
  368. certs crl newcerts private
  369. [root@node2 CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048) 生成私钥;
  370. Generating RSA private key, 2048 bit long modulus
  371. .......+++
  372. ..+++
  373. e is 65537 (0x10001)
  374. [root@node2 CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365
  375. 生成自签证书;
  376. You are about to be asked to enter information that will be incorporated
  377. into your certificate request.
  378. What you are about to enter is what is called a Distinguished Name or a DN.
  379. There are quite a few fields but you can leave some blank
  380. For some fields there will be a default value,
  381. If you enter '.', the field will be left blank.
  382. -----
  383. Country Name (2 letter code) [XX]:CN
  384. State or Province Name (full name) []:Beijing
  385. Locality Name (eg, city) [Default City]:Beijing
  386. Organization Name (eg, company) [Default Company Ltd]:magedu
  387. Organizational Unit Name (eg, section) []:devops
  388. Common Name (eg, your name or your server's hostname) []:www.magedu.com
  389. Email Address []:admin@magedu.com
  390. 为CA提供所需的目录及文件;
  391. [root@node2 CA]#touch index.txt
  392. [root@node2 CA]#echo 01 > serial
  393. [root@node2 CA]# ls
  394. cacert.pem crl certs index.txt newcerts private serial
  395. 用到证书的主机生成私钥;
  396. [root@node1 nginx]# mkdir /etc/nginx/ssl
  397. [root@node1 nginx]# cd /etc/nginx/ssl
  398. [root@node1 ssl]# ll
  399. total 0
  400. [root@node1 ssl]# (umask 077;openssl genrsa -out nginx.key 2048)
  401. Generating RSA private key, 2048 bit long modulus
  402. .....................................................................................................................................................+++
  403. .....+++
  404. e is 65537 (0x10001)
  405. 生成证书签署请求
  406. [root@node1 ssl]# openssl req -new -key nginx.key -out ngingx.csr
  407. You are about to be asked to enter information that will be incorporated
  408. into your certificate request.
  409. What you are about to enter is what is called a Distinguished Name or a DN.
  410. There are quite a few fields but you can leave some blank
  411. For some fields there will be a default value,
  412. If you enter '.', the field will be left blank.
  413. -----
  414. Country Name (2 letter code) [XX]:CN
  415. State or Province Name (full name) []:Beijing
  416. Locality Name (eg, city) [Default City]:Beijing
  417. Organization Name (eg, company) [Default Company Ltd]:magedu
  418. Organizational Unit Name (eg, section) []:devops
  419. Common Name (eg, your name or your server's hostname) []:www.magedu.com
  420. Email Address []:admin@magedu.com
  421. Please enter the following 'extra' attributes
  422. to be sent with your certificate request
  423. A challenge password []:
  424. An optional company name []:
  425. [root@node1 ssl]# ll
  426. total 8
  427. -rw-r--r--. 1 root root 1058 Nov 10 17:05 ngingx.csr
  428. -rw-------. 1 root root 1679 Nov 10 17:02 nginx.key
  429. [root@node1 ssl]#
  430. 将请求通过可靠方式发送给CA主机;
  431. [root@node1 ssl]# scp ngingx.csr 192.168.170.9:/tmp/
  432. root@192.168.170.9's password:
  433. ngingx.csr 100% 1058 61.9KB/s 00:00
  434. [root@node1 ssl]#
  435. 在CA主机上签署证书;
  436. [root@node2 CA]# openssl ca -in /tmp/nginx.csr -out /etc/pki/CA/certs/nginx.crt -days 365
  437. Using configuration from /etc/pki/tls/openssl.cnf
  438. Check that the request matches the signature
  439. Signature ok
  440. Certificate Details:
  441. Serial Number: 1 (0x1)
  442. Validity
  443. Not Before: Nov 10 09:24:59 2018 GMT
  444. Not After : Nov 10 09:24:59 2019 GMT
  445. Subject:
  446. countryName = CN
  447. stateOrProvinceName = Beijing
  448. organizationName = magedu
  449. organizationalUnitName = devops
  450. commonName = www.magedu.com
  451. emailAddress = tom@magedu.com
  452. X509v3 extensions:
  453. X509v3 Basic Constraints:
  454. CA:FALSE
  455. Netscape Comment:
  456. OpenSSL Generated Certificate
  457. X509v3 Subject Key Identifier:
  458. 4E:FB:76:35:75:55:17:84:C8:1A:58:5A:05:FE:42:9A:8F:A0:FE:97
  459. X509v3 Authority Key Identifier:
  460. keyid:C7:0A:BF:A0:F0:D3:BE:29:53:6E:96:8F:ED:6A:77:6D:D6:56:19:6F
  461. Certificate is to be certified until Nov 10 09:24:59 2019 GMT (365 days)
  462. Sign the certificate? [y/n]:y
  463. 1 out of 1 certificate requests certified, commit? [y/n]y
  464. Write out database with 1 new entries
  465. Data Base Updated
  466. 把签署好的证书发给请求者
  467. [root@node2 CA]# scp certs/nginx.crt 192.168.170.8:/etc/nginx/ssl
  468. The authenticity of host '192.168.170.8 (192.168.170.8)' can't be established.
  469. ECDSA key fingerprint is SHA256:ph7qUGHxmdPtYkXCbxolOLOERtICqxvsn5vNVWo/tGg.
  470. ECDSA key fingerprint is MD5:5a:80:04:d4:8e:6d:f5:15:2f:da:18:4e:45:9a:f4:2f.
  471. Are you sure you want to continue connecting (yes/no)? yes
  472. Warning: Permanently added '192.168.170.8' (ECDSA) to the list of known hosts.
  473. root@192.168.170.8's password:
  474. nginx.crt 100% 4621 2.7MB/s 00:00
  475. 做文件备份
  476. [root@node1 ssl]# cp /etc/nginx/conf.d/vhost.conf /etc/nginx/conf.d/vhost.conf.bak
  477. 配置nginx启用ssl功能
  478. [root@node1 ssl]# vi /etc/nginx/conf.d/vhost.conf
  479. server {
  480. listen 443 ssl;
  481. server_name www.node1.com;
  482. root /data/nginx/vhost;
  483. access_log /var/log/nginx/vhost_access.log main;
  484. ssl on;
  485. ssl_certificate /etc/nginx/ssl/nginx.crt;
  486. ssl_certificate_key /etc/nginx/ssl/nginx.key;
  487. ssl_protocols sslv3 tlsv1 tlsv1.1 tlsv1.2;
  488. ssl_session_cache shared:SSL:10m;
  489. location / {
  490. #root /data/nignx/vhost2;
  491. allow all;
  492. }
  493. location ~*\.(jpg|png)$ {
  494. deny 192.168.170.9;
  495. allow all;
  496. }
  497. location ~* ^/(admin|login) {
  498. auth_basic "admin area";
  499. auth_basic_user_file /etc/nginx/.ngxpasswd;
  500. }
  501. #location ^~ /images/ {
  502. # root /data/pictures/;
  503. #}
  504. location ^~ /images/ {
  505. alias /data/pictures/;
  506. }
  507. error_page 404 =202 /notfound.html;
  508. location = /notfound.html {
  509. root /data/nginx/error_pages;
  510. }
  511. location /ngxstatus {
  512. stub_status;
  513. }
  514. }
  515. "/etc/nginx/conf.d/vhost.conf" 40L, 948C written
  516. [root@node1 ssl]# nginx -t
  517. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  518. nginx: configuration file /etc/nginx/nginx.conf test is successful
  519. [root@node1 ssl]# nginx -s reload
  520. [root@node1 ssl]#
  521. [root@node1 ssl]# ss -tunlp
  522. Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
  523. tcp LISTEN 0 128 *:443 *:* users:(("nginx",pid=20180,fd=14),("nginx",pid=20179,fd=14),("nginx",pid=17585,fd=14))
  524. 浏览器输入https://192.168.170.8
  525. vhost
  526. 正常

二、 简述Linux集群类型、系统扩展方式及调度方法

  1. Cluster:计算机集合,为解决某个特定问题组合起来形成的单个系统;
  2. Linux Cluster类型:
  3. LB:Load Balancing,负载均衡;
  4. HA:High Availiablity,高可用;
  5. A=MTBF/(MTBF+MTTR)
  6. (0,1):90%, 95%, 99%, 99.5%, 99.9%, 99.99%, 99.999%, 99.9999%
  7. HP:High Performance,高性能;把计算复杂的问题,把计算量分散到多个CPU上。
  8. www.top500.org
  9. 分布式系统:
  10. 分布式存储,是将数据分散存储在多台独立的设备上。传统的网络存储系统采用集中的存储服务器存放所有数据,存储服务器成为系统性能的瓶颈,也是可靠性和安全性的焦点,不能满足大规模存储应用的需要。分布式网络存储系统采用可扩展的系统结构,利用多台存储服务器分担存储负荷,利用位置服务器定位存储信息,它不但提高了系统的可靠性、可用性和存取效率,还易于扩展。
  11. 分布式计算,是将计算很复杂的问题分散到多个计算机来处理,最后将结果汇总到一个计算机上显示
  12. 系统扩展方式:
  13. Scale UP:向上扩展,更换性能更强的主机,服务器能承受大量的用户请求,性价比低。
  14. Scale Out:向外扩展,增加主机数量使得客户端请求响应时,客户端请求能在多个服务器之间均衡分配。性价比高
  15. ipvs scheduler:
  16. 根据其调度时是否考虑各RS当前的负载状态,可分为静态方法和动态方法两种:
  17. 静态方法:仅根据算法本身进行调度;起点公平
  18. RR:roundrobin,轮询;
  19. WRR:Weighted RR,加权轮询;weight越大越优先
  20. SH:Source Hashing,实现session sticky,源IP地址hash;将来自于同一个IP地址的请求始终发往第一次挑中的RS,从而实现会话绑定,RS挂了会话丢失,可靠性不高;
  21. DH:Destination Hashing;目标地址哈希,将发往同一个目标地址的请求始终转发至第一次挑中的RS,典型使用场景是正向代理缓存场景中的负载均衡;
  22. 动态方法:主要根据每RS当前的负载状态及调度算法进行调度;结果公平
  23. Overhead=计算后端服务器当前的负载值
  24. LC:least connections 谁少挑选谁
  25. Overhead=activeconns*256+inactiveconns
  26. WLC:Weighted LC
  27. Overhead=(activeconns*256+inactiveconns)/weight
  28. SED:Shortest Expection Delay
  29. Overhead=(activeconns+1)*256/weight
  30. NQ:Never Queue
  31. LBLC:Locality-Based LC,动态的DH算法;
  32. LBLCR:LBLC with Replication,带复制功能的LBLC,服务器之间可以缓存项共享,使得lvs在失去平衡下拉回;

三、简述lvs四种集群有点及使用场景  

  1. lvs-nat:
  2. 多目标IP的DNAT,通过将请求报文中的目标地址和目标端口修改为某挑出的RS的RIP和PORT实现转发;
  3. 1)RIP和DIP必须在同一个IP网络,且应该使用私网地址;RS的网关要指向DIP;
  4. 2)请求报文和响应报文都必须经由Director转发;Director易于成为系统瓶颈;
  5. 3)支持端口映射,可修改请求报文的目标PORT;
  6. 4)vs必须是Linux系统,rs可以是任意系统;
  7. lvs-dr:
  8. Direct Routing,直接路由;
  9. 通过为请求报文重新封装一个MAC首部进行转发,源MAC是DIP所在的接口的MAC,目标MAC是某挑选出的RS的RIP所在接口的MAC地址;源IP/PORT,以及目标IP/PORT均保持不变;
  10. Director和各RS都得配置使用VIP;
  11. (1) 确保前端路由器将目标IP为VIP的请求报文发往Director:
  12. (a) 在前端网关做静态绑定;
  13. (b) 在RS上使用arptables;
  14. (c) 在RS上修改内核参数以限制arp通告及应答级别;
  15. arp_announce
  16. arp_ignore
  17. (2) RS的RIP可以使用私网地址,也可以是公网地址;RIP与DIP在同一IP网络;RIP的网关不能指向DIP,以确保响应报文不会经由Director;
  18. (3) RS跟Director要在同一个物理网络;
  19. (4) 请求报文要经由Director,但响应不能经由Director,而是由RS直接发往Client;
  20. (5) 不支持端口映射;
  21. lvs-tun:
  22. 转发方式:不修改请求报文的IP首部(源IP为CIP,目标IP为VIP),而是在原IP报文之外再封装一个IP首部(源IP是DIP,目标IP是RIP),将报文发往挑选出的目标RS;RS直接响应给客户端(源IP是VIP,目标IP是CIP);
  23. (1) DIP, VIP, RIP都应该是公网地址;
  24. (2) RS的网关不能,也不可能指向DIP;
  25. (3) 请求报文要经由Director,但响应不能经由Director;
  26. (4) 不支持端口映射;
  27. (5) RS的OS得支持隧道功能;
  28. lvs-fullnat:
  29. 通过同时修改请求报文的源IP地址和目标IP地址进行转发;
  30. CIP <--> DIP
  31. VIP <--> RIP
  32. (1) VIP是公网地址,RIP和DIP是私网地址,且通常不在同一IP网络;因此,RIP的网关一般不会指向DIP;
  33. (2) RS收到的请求报文源地址是DIP,因此,只能响应给DIP;但Director还要将其发往Client;
  34. (3) 请求和响应报文都经由Director;
  35. (4) 支持端口映射;
  36. 注意:此类型默认不支持;

                       
4、描述LVS-NAT、LVS-DR的工作原理并实现配置

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/684984
推荐阅读
相关标签
  

闽ICP备14008679号