百万架构师第四十二课:Nginx:Nginx 的初步认识|JavaGuide ...

打印 上一主题 下一主题

主题 873|帖子 873|积分 2619

百万架构师系列文章阅读体验感更佳

原文链接:https://javaguide.net

公众号:不止极客

Nginx 的初步认识及设置
课程目标


  • Nginx 在分布式架构中的应用分析
  • 常用的 Web 服务器及差异
  • Nginx 的安装以及设置分析
  • Nginx 虚拟主机设置
  • 详解 Location 的匹配规则
背景

早期用 F5 做负载均衡

后来通过负载均衡和热备来提高整个的 QPS。

什么是 Nginx

Nginx 是一个高性能的反向代理服务器

  • 正向代理代理的是客户端
  • 反向代理代理的是服务端
Apache、Tomcat、Nginx

​        都是 HTTP 服务器,他们都会放到我们服务端的服务器上,通过绑定 IP 和 端口的方式,去监听一个端口号。接收客户端的 HTTP 哀求。

展示给用户端。
JSP 是动态资源。
apache、nginx静态文件服务器css / jsTomcat动态文件服务器jsp / servletapache有一些性能瓶颈的Nginx主要是用来处理惩罚高并发的哀求Nginx 和 Apache 也是可以做动态解析的,但是它本身不是做这个的。
Nginx 功能


  • 反向代理。
  • 流量分发
  • 动静分离
  • 认证
  • 授权
它照旧一个高度模块化的设计
安装 Nginx

http://nginx.org/download/
  1. [root@Darian1 software]# mkdir nginx
  2. [root@Darian1 software]# cd nginx-1.1.4/
  3. [root@Darian1 nginx-1.1.4]# ./configure --prefix=/software/nginx
  4. ./configure: error: the HTTP rewrite module requires the PCRE library.
  5. You can either disable the module by using --without-http_rewrite_module
  6. option, or install the PCRE library into the system, or build the PCRE library
  7. statically from the source with nginx by using --with-pcre=<path> option.
  8.   
  9. [root@Darian1 nginx-1.1.4]# yum install pcre-devel
  10. [root@Darian1 nginx-1.1.4]# ./configure --prefix=/software/nginx
  11. ./configure: error: the HTTP gzip module requires the zlib library.
  12. You can either disable the module by using --without-http_gzip_module
  13. option, or install the zlib library into the system, or build the zlib library
  14. statically from the source with nginx by using --with-zlib=<path> option.
  15.    
  16. [root@Darian3 nginx-1.4.1]# yum install -y zlib-devel
  17. [root@Darian1 nginx-1.1.4]# ./configure --prefix=/software/nginx
  18. [root@Darian1 nginx-1.1.4]# make && make install
  19. [root@Darian1 software]# cd nginx
  20. [root@Darian1 nginx]# ll
  21. 总用量 4
  22. drwxr-xr-x. 2 root root 4096 1月  17 13:58 conf
  23. drwxr-xr-x. 2 root root   68 1月  17 13:58 html
  24. drwxr-xr-x. 2 root root    6 1月  17 13:58 logs
  25. drwxr-xr-x. 2 root root   36 1月  17 13:59 sbin
  26. [root@Darian1 nginx]# cd sbin/
  27. [root@Darian1 sbin]# ll
  28. 总用量 1704
  29. -rwxr-xr-x. 1 root root 868424 1月  17 13:59 nginx
  30. -rwxr-xr-x. 1 root root 868424 1月  17 13:58 nginx.old
  31. [root@Darian1 sbin]# ./nginx
  32. [root@Darian1 sbin]# ./nginx -s stop
复制代码

  • 下载 tar 包
  • tar -zxvf nginx.tar.gz
  • ./configure [--prefix]
  • make && make install
启动和停止


  • ./nginx
  • ./nginx -s stop

nginx.conf
  1. [root@Darian1 nginx]# cd conf/
  2. [root@Darian1 conf]# ll
  3. 总用量 60
  4. -rw-r--r--. 1 root root  979 1月  17 13:58 fastcgi.conf
  5. -rw-r--r--. 1 root root  979 1月  17 13:59 fastcgi.conf.default
  6. -rw-r--r--. 1 root root  909 1月  17 13:58 fastcgi_params
  7. -rw-r--r--. 1 root root  909 1月  17 13:59 fastcgi_params.default
  8. -rw-r--r--. 1 root root 2837 1月  17 13:59 koi-utf
  9. -rw-r--r--. 1 root root 2223 1月  17 13:59 koi-win
  10. -rw-r--r--. 1 root root 3268 1月  17 13:58 mime.types
  11. -rw-r--r--. 1 root root 3268 1月  17 13:59 mime.types.default
  12. -rw-r--r--. 1 root root 2685 1月  17 13:58 nginx.conf
  13. -rw-r--r--. 1 root root 2685 1月  17 13:59 nginx.conf.default
  14. -rw-r--r--. 1 root root  544 1月  17 13:58 scgi_params
  15. -rw-r--r--. 1 root root  544 1月  17 13:59 scgi_params.default
  16. -rw-r--r--. 1 root root  570 1月  17 13:58 uwsgi_params
  17. -rw-r--r--. 1 root root  570 1月  17 13:59 uwsgi_params.default
  18. -rw-r--r--. 1 root root 3610 1月  17 13:59 win-utf
  19. [root@Darian1 conf]# vim nginx.conf
复制代码
  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. #pid        logs/nginx.pid;
  7. events {
  8.     # 允许连接的最大数量
  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.     sendfile        on;
  19.     #tcp_nopush     on;
  20.     #keepalive_timeout  0;
  21.     keepalive_timeout  65;
  22.     #gzip  on;
  23.     server {
  24.         listen       80;
  25.         server_name  localhost;
  26.         #charset koi8-r;
  27.         #access_log  logs/host.access.log  main;
  28.         location / {
  29.             root   html;
  30.             index  index.html index.htm;
  31.         }
  32.         #error_page  404              /404.html;
  33.         # redirect server error pages to the static page /50x.html
  34.         #
  35.         error_page   500 502 503 504  /50x.html;
  36.         location = /50x.html {
  37.             root   html;
  38.         }
  39.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  40.         #
  41.         #location ~ \.php$ {
  42.         #    proxy_pass   http://127.0.0.1;
  43.         #}
  44.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  45.         #
  46.         #location ~ \.php$ {
  47.         #    root           html;
  48.         #    fastcgi_pass   127.0.0.1:9000;
  49.         #    fastcgi_index  index.php;
  50.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  51.         #    include        fastcgi_params;
  52.         #}
  53.         # deny access to .htaccess files, if Apache's document root
  54.         # concurs with nginx's one
  55.         #
  56.         #location ~ /\.ht {
  57.         #    deny  all;
  58.         #}
  59.     }
  60.     # another virtual host using mix of IP-, name-, and port-based configuration
  61.     #
  62.     #server {
  63.     #    listen       8000;
  64.     #    listen       somename:8080;
  65.     #    server_name  somename  alias  another.alias;
  66.     #    location / {
  67.     #        root   html;
  68.     #        index  index.html index.htm;
  69.     #    }
  70.     #}
  71.     # HTTPS server
  72.     #
  73.     #server {
  74.     #    listen       443;
  75.     #    server_name  localhost;
  76.     #    ssl                  on;
  77.     #    ssl_certificate      cert.pem;
  78.     #    ssl_certificate_key  cert.key;
  79.     #    ssl_session_timeout  5m;
  80.     #    ssl_protocols  SSLv2 SSLv3 TLSv1;
  81.     #    ssl_ciphers  HIGH:!aNULL:!MD5;
  82.     #    ssl_prefer_server_ciphers   on;
  83.     #    location / {
  84.     #        root   html;
  85.     #        index  index.html index.htm;
  86.     #    }
  87.     #}
  88. }
复制代码
Main

event

http

虚拟主机设置

针对不同的端口号做不同的访问,不同域名做不同的访问。
  1. server {
  2.     listen 80;  
  3.     server_name localhost;
  4.     #charset koi8-r;
  5.     #access_log logs/host.access.log main;
  6.     location / {
  7.         root html;
  8.         index index.html index.htm;
  9.     }
  10. }
复制代码
基于ip的虚拟主机

不演示
基于端口号的虚拟主机
  1. server {
  2.     listen 8080;
  3.     server_name localhost;
  4.     location / {
  5.         root html;
  6.         index index.html;
  7.     }
  8. }
复制代码
我们可以设置一个端口段,去做不同的路由转发。
基于域名的虚拟主机

购买主域名以后,可以基于主域名开放很多二级域名。
www.darian.com / ask.darian.com / git.darian.com / bbs.darian.com
  1. server{
  2.     listen 8080;
  3.     server_name localhost;
  4.     location / {
  5.         root html;
  6.         index index.html;
  7.     }
  8. }
  9. server{
  10.     listen 80;
  11.     server_name bbs.darian.com;
  12.     location / {
  13.         root html;
  14.         index bbs.html;
  15.     }
  16. }
  17. server{
  18.     listen 80;
  19.     server_name ask.darian.com;
  20.     location / {
  21.         root html;
  22.         index ask.html;
  23.     }
  24. }
  25. server{
  26.     listen 80;
  27.     server_name www.darian.com;
  28.     location / {
  29.         # 去这个目录下边找
  30.         root html;
  31.         index index.html;
  32.     }
  33. }
复制代码
  1. [root@Darian1 nginx]# ./sbin/nginx
  2. [root@Darian1 nginx]# ./sbin/nginx -s reload
  3. [root@Darian1 nginx]# ps -ef|grep nginx
  4. root       9595      1  0 15:16 ?        00:00:00 nginx: master process ./sbin/nginx
  5. nobody     9596   9595  0 15:16 ?        00:00:00 nginx: worker process
  6. root       9619   9550  0 15:28 pts/2    00:00:00 grep --color=auto nginx
复制代码
必要去 html 目录下创建几个文件
  1. [root@Darian1 html]# ll
  2. 总用量 24
  3. -rw-r--r--. 1 root root  383 1月  17 13:58 50x.html
  4. -rw-r--r--. 1 root root   26 1月  17 16:13 ask.html
  5. -rw-r--r--. 1 root root   25 1月  17 16:14 bbs.html
  6. -rw-r--r--. 1 root root  151 1月  17 13:58 index.html
  7. -rw-r--r--. 1 root root 7133 1月  17 13:58 ngx_core_module.html
复制代码
可能必要刷新 DNS




用本地 host 实现了域名的解析。
二级域名随意创造,存案主域名就好了
建议

将设置单独抽出来,放到一个地方去同一地进行管理。
代理办理的事变


  • 负载均衡
  • 缓存
  • 限流
  • 表里网的隔离,做安全性的东西
location

​        nginx 里边,必要知道 location ,对 location 做一些哀求的转发。
根据哀求的 URI 匹配到对应的 location 以后,去做对应的转发。
设置语法
  1. location [= | ~* | ^~ ] /uri/ {...}
复制代码
设置规则
  1. location = /uri 精准匹配
  2. location ^~ /uri 前缀匹配
  3. location ~ /uri
  4. location / 通用匹配
复制代码
规则的优先级
  1. 1 location = /
  2. 2 location = /index
  3. 3 location ^~ /article/
  4. 4 location ^~ /article/files/
  5. 5 location ~ \.(gif|png|js|css)$
  6. 6 location /
  7.    
  8. http://192.168.11.154/   -> 1
  9. http://192.168.11.154/index ->2
  10. http://192.168.11.154/article/files/1.txt ->4
  11. http://192.168.11.154/darian.png ->5
复制代码
(静态资源服务器,可以做压缩的)
匹配规则是不能重复的。

  • 精准匹配是优先级最高
  • 普通匹配(最长的匹配)
  • 正则匹配
现实利用建议
  1. # 根目录的配置规则,首页是静态页面
  2. location =/ {
  3. }
  4. # 通用匹配  http://192.168.40.128:8080/index.html
  5. location / {
  6. }
  7. # 静态规则,做动静分离
  8. location ~* \.(gif|....)${
  9. }
复制代码
Nginx模块

​        Nginx 内部都是由一些核心和非核心的第三方模块组成的。它的模块化,我们可以做一些动态的集成和扩展。
​        除了它本身的功能以外,我们还可以利用第三方的设置去实现更复杂的功能。

默认的:

  • 反向代理
  • email
  • nginx core
模块分类


  • 核心模块 ngx_http_core_module
  • 尺度模块 http模块
  • 第三方模块
可以把 模块当作插件
ngx_http_core_module
  1. server{
  2.     listen port
  3.         server_name   ...
  4.         localtion{
  5.         root ...
  6.     }
  7. }
复制代码
Location 实现 URI 到文件系统路径的映射

  • error_page
    1. # redirect server error pages to the static page /50x.html
    2. #
    3. error_page   500 502 503 504  /50x.html;
    4. location = /50x.html {
    5.     root   html;
    6. }
    复制代码
ngx_http_access_module

实现基于 IP 的访问控制功能

  • allow address | CIDR | unix: | all;
  • deny address | CIDR | unix: | all;

自上而下查抄,一旦匹配,将生效,条件严格的置前
可以用 -t 来看语法有没有问题
  1. [root@Darian1 sbin]# ./nginx -t
复制代码
如何添加第三方模块

Nginx 不支持动态去安装或者加载模块。安装别的代码的时间必要重新编译。(我们很少去编写模块)

  • 原来所安装的设置,你必须在重新安装新模块的时间,加上
  • 不能直接 make install
  1. configure --prefix=/data/program/nginx
复制代码
安装方法
  1. ./configure --prefix=/安装目录 --add-module = /第三方模块的目录
  2. ./configure --prefix=/data/program/nginx --with-http_stub_status_module --with-http_random_index_module
  3. cp objs/nginx $nginx_home/sbin/nginx
复制代码
  1. [root@Darian1 nginx-1.1.4]# cd ../nginx-1.1.4/
  2. [root@Darian1 nginx-1.1.4]# ./configure --prefix=/software/nginx --with-http_stub_status_module --with-http_random_index_module
  3. [root@Darian1 nginx-1.1.4]# ps -ef|grep nginx
  4. root       9595      1  0 15:16 ?        00:00:00 nginx: master process ./sbin/nginx
  5. nobody     9755   9595  0 16:25 ?        00:00:00 nginx: worker process
  6. root      13696   9550  0 19:35 pts/2    00:00:00 grep --color=auto nginx
  7. [root@Darian1 nginx-1.1.4]# kill -9 9595
  8. [root@Darian1 nginx-1.1.4]# kill -9 9755
  9. [root@Darian1 nginx-1.1.4]# ps -ef|grep nginx
  10. root      13699   9550  0 19:35 pts/2    00:00:00 grep --color=auto nginx
  11. [root@Darian1 nginx-1.1.4]# cp objs/nginx ../nginx/sbin/nginx
  12. cp:是否覆盖"../nginx/sbin/nginx"? y
  13. [root@Darian1 nginx-1.1.4]#
复制代码
如果直接 make && make install 会直接就没了。
以是 更换。
  1. [root@Darian1 nginx-1.1.4]# kill -9 13713
  2. [root@Darian1 nginx-1.1.4]# ./configure --prefix=/software/nginx --with-http_stub_status_module --with-http_random_index_module
  3. [root@Darian1 nginx-1.1.4]# make
  4. [root@Darian1 nginx-1.1.4]# cp objs/nginx ../nginx/sbin/nginx
  5. cp:是否覆盖"../nginx/sbin/nginx"? y
复制代码
http_stub_status_module
  1. location /status{
  2.     stub_status on;
  3. }
复制代码


  • Active connections:当前状态,活动状态的连接数
  • accepts:统计总值,已经接收的客户端哀求的总数
  • handled:统计总值,已经处理惩罚完成的客户端哀求的总数
  • requests:统计总值,客户端发来的总的哀求数
  • Reading:当前状态,正在读取客户端哀求报文首部的连接的连接数
  • Writing:当前状态,正在向客户端发送响应报文过程中的连接数
  • Waiting:当前状态,正在等待客户端发出哀求的空闲连接数
http_random_index_module

www.darian.com
随机显示主页
​        一般情况下,一个站点默认首页都是定义好的 index.html 、 index.shtml 等等,如果站点下有很多页面想随机展示给用户浏览,那得步伐上实现,很麻烦,利用 nginx 的 random index 即可简朴实现这个功能,凡是以/结尾的哀求,都会随机展示当前目录下的文件作为首页

  • 添加 random_index on 设置,默认是关闭的
    1. location / {
    2.     root html;
    3.     random_index on;
    4.     index index.html index.htm;
    5. }
    复制代码
  • 在 html 目录下创建多个 html 页面
他就会多个 HTML 页面随机地展示在首页。
( 官方的模块,你可以直接通过 with model 去集成,如果是下载第三方插件可以通过 http model 去集成。 )
百万架构师系列文章阅读体验感更佳

原文链接:https://javaguide.net

公众号:不止极客

泉源于:  https://javaguide.net
微信公众号:不止极客

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

知者何南

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表