Tengine 的编译安装

1 选定目录以及解压文件

  1. cd /opt
  2. wget http://tengine.taobao.org/download/tengine-2.3.3.tar.gz

2 编译安装

  1. cd tengine-2.3.3
  2. ./configure --prefix=/opt/tengine233
  3. make && make install

配置文件的讲解

content

  1. cd tengin233
  2. ls
  3. conf html logs sbin
  4. conf --- 存放配置文件
  5. html --- 存放网页的静态文件目录
  6. logs --- 日志
  7. sbin --- 存放可执行命令

environment variable—quick start

  1. Nginx
  2. /opt/tengine233/sbin
  3. system
  4. /opt/python396/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin
  5. PATH="/opt/tengine233/sbin:/opt/python396/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin"
  6. vim /etc/profile

start nginx

  1. /opt/tengine233/sbin/nginx
  2. # after add environment variable
  3. nginx

check port occupied

  1. netstat -tunlp | grep 80
  2. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13902/nginx: master
  3. lsof -i :80
  4. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  5. nginx 13902 root 6u IPv4 132914 0t0 TCP *:http (LISTEN)
  6. nginx 13903 nobody 6u IPv4 132914 0t0 TCP *:http (LISTEN)

usual command

  1. nginx # 首次输入是直接启动,不得再次输入
  2. nginx -s reload # 平滑重启,重新读取nginx的配置文件,而不重启进程
  3. nginx -s stop # 停止nginx进程
  4. nginx -t # 检查配置文件 相当于 pytho
  1. nginx -t
  2. nginx: the configuration file /opt/tengine233/conf/nginx.conf syntax is ok
  3. nginx: configuration file /opt/tengine233/conf/nginx.conf test is successful

默认首页

http://218.78.112.182:10001/index.html

Nginx配置文件的学习

简介

  1. 1. 代码风格
  2. C语言,分号结束,表示每一行的的配置;
  3. 2. 代码块
  4. 1. http {} # 定义多个代码,是Nginx核心功能配置点
  5. 2. server {} # 虚拟主机代码,定义了网站的目录地址,以及首页文件名字,监听端口等功能
  6. 3. localtion {} # 域名匹配代码块

功能代码结构

  1. http {
  2. server{
  3. listen 80;
  4. servername www.whereabouts.com;
  5. location / {
  6. }
  7. }
  8. }

默认配置文件备份

配置文件内有两份全站配置 http,但是第二份完全被注释,因此修改第一份 http 即可

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #error_log "pipe:rollback logs/error_log interval=1d baknum=7 maxsize=2G";
  7. #pid logs/nginx.pid;
  8. events {
  9. worker_connections 1024;
  10. }
  11. http {
  12. include mime.types;
  13. default_type application/octet-stream;
  14. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  15. # '$status $body_bytes_sent "$http_referer" '
  16. # '"$http_user_agent" "$http_x_forwarded_for"';
  17. #access_log logs/access.log main;
  18. #access_log "pipe:rollback logs/access_log interval=1d baknum=7 maxsize=2G" main;
  19. sendfile on;
  20. #tcp_nopush on;
  21. #keepalive_timeout 0;
  22. keepalive_timeout 65;
  23. #gzip on;
  24. server {
  25. listen 80;
  26. server_name localhost;
  27. #charset koi8-r;
  28. #access_log logs/host.access.log main;
  29. #access_log "pipe:rollback logs/host.access_log interval=1d baknum=7 maxsize=2G" main;
  30. location / {
  31. root html;
  32. index index.html index.htm;
  33. }
  34. #error_page 404 /404.html;
  35. # redirect server error pages to the static page /50x.html
  36. #
  37. error_page 500 502 503 504 /50x.html;
  38. location = /50x.html {
  39. root html;
  40. }
  41. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  42. #
  43. #location ~ \.php$ {
  44. # proxy_pass http://127.0.0.1;
  45. #}
  46. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  47. #
  48. #location ~ \.php$ {
  49. # root html;
  50. # fastcgi_pass 127.0.0.1:9000;
  51. # fastcgi_index index.php;
  52. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  53. # include fastcgi_params;
  54. #}
  55. # pass the Dubbo rpc to Dubbo provider server listening on 127.0.0.1:20880
  56. #
  57. #location /dubbo {
  58. # dubbo_pass_all_headers on;
  59. # dubbo_pass_set args $args;
  60. # dubbo_pass_set uri $uri;
  61. # dubbo_pass_set method $request_method;
  62. #
  63. # dubbo_pass org.apache.dubbo.samples.tengine.DemoService 0.0.0 tengineDubbo dubbo_backend;
  64. #}
  65. # deny access to .htaccess files, if Apache's document root
  66. # concurs with nginx's one
  67. #
  68. #location ~ /\.ht {
  69. # deny all;
  70. #}
  71. }
  72. # upstream for Dubbo rpc to Dubbo provider server listening on 127.0.0.1:20880
  73. #
  74. #upstream dubbo_backend {
  75. # multi 1;
  76. # server 127.0.0.1:20880;
  77. #}
  78. # another virtual host using mix of IP-, name-, and port-based configuration
  79. #
  80. #server {
  81. # listen 8000;
  82. # listen somename:8080;
  83. # server_name somename alias another.alias;
  84. # location / {
  85. # root html;
  86. # index index.html index.htm;
  87. # }
  88. #}
  89. # HTTPS server
  90. #
  91. #server {
  92. # listen 443 ssl;
  93. # server_name localhost;
  94. # ssl_certificate cert.pem;
  95. # ssl_certificate_key cert.key;
  96. # ssl_session_cache shared:SSL:1m;
  97. # ssl_session_timeout 5m;
  98. # ssl_ciphers HIGH:!aNULL:!MD5;
  99. # ssl_prefer_server_ciphers on;
  100. # location / {
  101. # root html;
  102. # index index.html index.htm;
  103. # }
  104. #}
  105. }

nginxweb站点

首页静态文件的存放路径

/opt/tengine233/html

默认主页源码保存

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Welcome to tengine!</title>
  5. <style>
  6. body {
  7. width: 35em;
  8. margin: 0 auto;
  9. font-family: Tahoma, Verdana, Arial, sans-serif;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <h1>Welcome to tengine!</h1>
  15. <p>If you see this page, the tengine web server is successfully installed and
  16. working. Further configuration is required.</p>
  17. <p>For online documentation and support please refer to
  18. <a href="http://tengine.taobao.org/">tengine.taobao.org</a>.</p>
  19. <p><em>Thank you for using tengine.</em></p>
  20. </body>
  21. </html>

自定义个人主页以及图片存储路径

  1. # 存放路径
  2. [root@vvkt7whznuckhiz2-0723575 html]# ls
  3. 50x.html index.html smooth.jpg
  4. [root@vvkt7whznuckhiz2-0723575 html]# pwd
  5. /opt/tengine233/html
  6. [root@vvkt7whznuckhiz2-0723575 html]# vim index.html
  7. <!DOCTYPE html>
  8. <body>
  9. <h4>Thinking less , practice more!<h4>
  10. <img src="./smooth.jpg">
  11. </body>

虚拟主机

Nginx - 图1

第二个 server

完整配置文件

  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. sendfile on;
  9. keepalive_timeout 65;
  10. gzip on;
  11. server {
  12. listen 10001;
  13. server_name localhost;
  14. charset utf-8;
  15. location / {
  16. root /s25linux/;
  17. index index.html index.htm;
  18. }
  19. error_page 500 502 503 504 /50x.html;
  20. location = /50x.html {
  21. root html;
  22. }
  23. }
  24. server {
  25. listen 40001;
  26. server_name localhost;
  27. charset utf-8;
  28. location / {
  29. root /s25python/;
  30. index index.html index.htm;
  31. }
  32. }
  33. }

外部配置文件写入

  1. echo "I love you, you love me, mixue ice city lovely" > /s25nginx/index.html

最终效果

Nginx - 图2

创建显示网页

  1. echo "I love you, you love me, mixue icecream honey and tea" > /s25linux/index.html
  2. echo "You watch my eyes, listen to me,not afraid of wind and rain." > /s25python/index.html

最终显示效果

Nginx - 图3

404 页面设计

配置文件的修改

  1. server {
  2. listen 10001;
  3. server_name localhost;
  4. charset utf-8;
  5. # 添加此行代码,当用户请求出错时,出现404的时候,会返回特定的页面,在root定义的根目录下寻找40x.html文件
  6. #
  7. error_page 404 /40x.html;
  8. #access_log logs/host.access.log main;
  9. #access_log "pipe:rollback logs/host.access_log interval=1d baknum=7 maxsize=2G" main;
  10. location / {
  11. root /s25linux/;
  12. index index.html index.htm;
  13. }
  14. }

预备修改的资源文件 40x.html

  1. cd /s25linux
  2. touch 40x.html
  3. vim 40x.html

H5 资源文件

  1. <!DOCTYPE html>
  2. <html lang="en" >
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>404</title>
  6. </head>
  7. <body>
  8. <h4>"What you see is what you get, but this page is not exist."</h4>
  9. </body>
  10. </html>

检查并且软重启 Nginx

  1. nginx -s reload
  2. # 或者关闭并启动
  3. nginx -s stop
  4. nginx

最终效果

Nginx - 图4

Nginx 访客日志

定义及用途

  1. 1. 特点:能够记录,分析用户的请求行为
  2. 什么时间点访问最频繁,网站流量峰值
  3. 记录用户的请求频率,依次检测是否为爬虫等恶意请求,进行封禁;
  4. 检测躲在用户代理后的真实IP
  5. 检测用户IP,请求时间,请求URL内容;
  6. 2. 格式
  7. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  8. # '$status $body_bytes_sent "$http_referer" '
  9. # '"$http_user_agent" "$http_x_forwarded_for"';
  10. 3. 对变量的解释

反向代理

正向代理相当于业务代办,面向用户,也就是服务需求方
反向代理相当于中介,面向房东,也就是服务提供方

负载均衡

为什么做负载均衡?[静态资源加速 & 流量平衡]

  1. 1. 代理服务器负载均衡 使用代理服务器,可以将请求转发给内部的服务器,使用这种加速模式显然可以提升静态网页的访问速度。
  2. 2. 使用代理服务器将请求均匀转发给多台服务器,从而达到负载均衡的目的。

实操

实现逻辑

Nginx - 图5

1 修改 nginx.conf配置,构建代理池

  1. upstream s25real_server {
  2. server 192.168.1.9:5555;
  3. server 192.168.1.9:6666;
  4. }

第一个主机10001端口用作网关,交给代理池

  1. server {
  2. listen 10001;
  3. server_name localhost;
  4. charset utf-8;
  5. location / {
  6. proxy_pass http://s25real_server;
  7. }
  8. }

第二个server 5555端口负责提供数据

  1. server {
  2. listen 5555;
  3. server_name localhost;
  4. charset utf-8;
  5. location / {
  6. root /s25python/;
  7. index index.html index.htm;
  8. }
  9. }

第三个server 6666依然是为了提供资源

  1. server {
  2. listen 5555;
  3. server_name localhost;
  4. charset utf-8;
  5. location / {
  6. root /s25linux/;
  7. index index.html index.htm;
  8. }
  9. }

2 完整配置文件

  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. sendfile on;
  9. keepalive_timeout 65;
  10. gzip on;
  11. upstream s25real_server {
  12. server 192.168.1.9:5555;
  13. server 192.168.1.9:6666;
  14. }
  15. server {
  16. listen 10001;
  17. server_name localhost;
  18. charset utf-8;
  19. location / {
  20. proxy_pass http://s25real_server;
  21. }
  22. error_page 500 502 503 504 /50x.html;
  23. location = /50x.html {
  24. root html;
  25. }
  26. }
  27. server {
  28. listen 5555;
  29. server_name localhost;
  30. charset utf-8;
  31. location / {
  32. root /s25python/;
  33. index index.html index.htm;
  34. }
  35. }
  36. server {
  37. listen 6666;
  38. server_name localhost;
  39. charset utf-8;
  40. location / {
  41. root /s25linux/;
  42. index index.html index.htm;
  43. }
  44. }
  45. }

集群

概念

特点

1, 高性能;

2,价格有效性;

  1. 采用计算机集群比采用同等运算性能的大型计算机更具有性价比

3, 可伸缩性;

  1. 硬件的扩展性强,且不影响面向服务的连续性;

4, 透明性;

负载均衡算法

  1. 默认轮询机制,每一台服务器处理一次访问请求;
  2. 加权轮询,给代理池的每一个主机分配权重,比如 weight=4; weight=1;
    upstream s25real_server {
    server 192.168.1.9:5555 weight=4;
    server 192.168.1.9:6666 weight=1;
    }

报错及处理

nginx: [error] invalid PID number ““ in “**/nginx/logs/nginx.pid

  1. 解决方法 :
  2. 进入到nginx的安装目录位置,进入sbin
  3. 执行 nginx -t 检查配置是否正确
  4. 看到 successful 说明配置没问题,如果不是,好好检查nginx.conf
  5. 之后执行以下两条命令:
  6. ./nginx -c /opt/tengine233/conf/nginx.conf
  7. ./nginx -s reload
  8. 修后使用以下命令,查看nginx进程:
  9. ps -ef|grep nginx
  10. 原文链接:https://blog.csdn.net/qq_36949163/article/details/119874257
  1. https://www.cnblogs.com/pyyu/p/10009493.html
  2. https://blog.csdn.net/lmiao1992/article/details/97516293?utm_term=yum%E9%95%9C%E5%83%8F%E6%B2%A1%E6%9C%89redis&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduweb~default-1-97516293&spm=3001.4430

图片.png