• Nginx网站服务


    一、Nginx概述

    1.1 Nginx介绍

    Nginx是一款高性能、轻量级的web服务软件

    优点:
    高性能:对http并发连接的处理能很高,单台物理服务器可支持30000-50000个并发请求(在实际操作中,为了维持服务器的稳定,一般设置在20000个左右)
    轻量级:nginx软件很小,安装所需的空间也很小
    稳定性强:对系统资源消耗低

    1.2 Nginx的主要功能

    1、处理静态网页:html、htm图像
    2、支持反向代理(负载均衡),负载均衡靠算法实现
    3、处理动态内容:但是处理能力较差,有专门处理动态内容的程序,例如comcat或srpingclound
    4、虚拟主机:nginx可以配置多个虚拟主机,每个虚拟主机都可以作为一个域名和站点,每个虚拟主机都可以拥有独立的配置和资源
    5、URL重定向:可以对URL的请求进行修改和重定向
    6、缓存机制,可以缓存静态文件和动态内容
    7、日志记录:服务日志,访问日志和报错日志:/usr/local/nginx/logs(控制日志还是在/var/log/messgaes)中
    8、可以做为代理服务器,通过代理可以访问其他的后端服务器(淘宝使用的就是基于nginx二次开发的Tengine天极)

    1.3 Nginx相关文件介绍

    1. conf:保存nginx的所有配置文件,其中nginx.conf是nginx的主配置文件
    2. html:保存nginx的web文件,html结尾的文件,图片
    3. 50x:nginx默认的报错提示页面
    4. logs:保存日志的目录,路径可以改
    5. access.log:记录访问日志记录
    6. error.log:记录报错日志
    7. sbin:nginx的二进制启动脚本

    1.4 Nginx常用选项

    1. -t:检测nginx配置文件的配置语法是否正确
    2. -v:只查看版本
    3. -V:查看版本和bginx支持的配置模块
    4. -s:给nginx主程序发送信号
    5. -s stop:停止nginx服务器,即停止监听连接的处理
    6. -s restart:重启nginx服务器,停止并重新启动监听连接的处理
    7. -s reload:重新加载nginx配置文件,实现平滑地重新加载配置文件,对新的配置进行生效,而无需停止正在处理的连接

    1.5 Nginx配置文件内容

    主模块:
    包含一些全局的指令,如工作进程数、日志文件路径等
    events模块:
    控制并发数
    httpd模块:
    配置代理、缓存、日志、虚拟主机和第三方模块
    server模块:
    配置虚拟主机的设备,在http的模块中可以有多个server
    location模块:
    location模块只能配置在server模块当中,匹配uri
    (location模块中root指定和alias指定的区别:root是拼接,alias是完整路径匹配)
    注:一个http中可以有多个server,一个server可以有多个location

    1.6 Nginx的功能模块

    proxy模块

    代理功能,核心功能模块之一,配置反向的功能

    proxy_pass:指定,定义在location当中

    headers模块

    处理请求头部的响应信息,获取客户端真实ip

    upstream模块

    七层反向代理模块,只能配置在http模块当中,或者stream模块中

    stream模块

    四层反向代理模块,只能写在配置当中

    二、Nginx的安装

    关闭防火墙及安全机制

    1. systemctl stop firewalld
    2. setenforce 0

    安装依赖环境

    yum -y install gcc pcre-devel openssl-devel zlib-devel openssl openssl-devel

    创建运行用户,指定nginx为程序用户不能登录

    useradd -M -s /sbin/nologin nginx

    编译安装

    1. cd /opt #然后将nginx压缩包传入系统
    2. tar -xf nginx-1.22.o.tar.gz
    3. cd nginx-1.22.0/
    4. ./configure --prefix=/usr/local/nginx \
    5. --user=nginx \
    6. --group=nginx \
    7. --with-http_ssl_module \
    8. --with-http_v2_module \
    9. --with-http_realip_module \
    10. --with-http_stub_status_module \
    11. --with-http_gzip_static_module \
    12. --with-pcre \
    13. --with-stream \
    14. --with-stream_ssl_module \
    15. --with-stream_realip_module
    16. make -j 4 && make install
    17. 注释:
    18. ./configure --prefix=/usr/local/nginx \ #指定安装目录
    19. --user=nginx \ #指定运行用户
    20. --group=nginx \ #指定运行组
    21. --with-http_ssl_module \ #开启对http的ssl加密支持
    22. --with-http_v2_module \ #支持http2.0协议
    23. --with-http_realip_module \ #允许nginx获取客户端的真实ip
    24. --with-http_stub_status_module \ #启动了stud_status模块,获取nginx的访问和状态信息的方法
    25. --with-http_gzip_static_module \ #支持压缩文件内容图片
    26. --with-pcre \ #动态库
    27. --with-stream \ #开启tcp/udp代理模块支持 支持四层转发
    28. --with-stream_ssl_module \ #支持四层转发的ssl加密
    29. --with-stream_realip_module #nginx可以从四层转发的头部信息中获取客户端的真实ip

    修改目录权限
    将/usr/local/nginx目录下的所有文件和子目录的所有者和所属组改为 nginx

    chown -R nginx.nginx /usr/local/nginx/

    创建软连接,让系统能够识别二进制

    ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

    添加 Nginx 系统服务

    1. vim /lib/systemd/system/nginx.service
    2. [Unit]
    3. Description=nginx - high performance web server
    4. Documentation=http://nginx.org/en/docs/
    5. After=network-online.target remote-fs.target nss-lookup.target
    6. Wants=network-online.target
    7. [Service]
    8. Type=forking
    9. PIDFile=/usr/local/nginx/run/nginx.pid
    10. ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    11. ExecReload=/bin/kill -s HUP $MAINPID
    12. ExecStop=/bin/kill -s TERM $MAINPID
    13. [Install]
    14. WantedBy=multi-user.target

    指定存储Nginx进程ID的文件路径 

    1. cd /usr/local/nginx/
    2. mkdir run
    3. chown -R nginx.nginx /usr/local/nginx
    4. cd conf
    5. vim nginx.conf
    6. pid /usr/local/nginx/run/nginx.pid;
    7. #把pid这一行的注释删掉,把路径改为设置好的路径

    重新加载配置并重启nginx服务

    1. nginx -t
    2. systemctl daemon-reload
    3. systemctl restart nginx

    三、实验 

    统计nginx的访问状态

    1. [root@localhost conf]# vim nginx.conf
    2. location /status {
    3. stub_status on;
    4. access_log off;
    5. }
    6. [root@localhost conf]# nginx -t
    7. [root@localhost conf]# systemctl restart nginx.service

    使用浏览器访问20.0.0.61/status

    Active connections:表示当前活动连接数

    server accepts:已经处理的连接数
    handled:成功的tcp握手次数
    requests:已经处理的请求数

    Reading:0:服务端正在从客户端读取数据,正在读取客户端请求的连接数
    Writing:1:服务器正在将响应数据发送给客户端,正在向客户端写入响应的连接数
    Wating:1:表示有连接处于空闲状态,当前等待客户端请求的连接数,即time_wait

    基于域名配置nginx的虚拟主机

    [root@pup1 conf]# vim nginx.conf

    添加新的server模块

    1. [root@localhost conf]# cd /var/www/html
    2. [root@localhost html]# mkdir pup accp
    3. [root@localhost html]# cd pup
    4. [root@localhost pup]# vim index.html
    5. this is pup!
    6. [root@localhost pup]# cd ..
    7. [root@localhost html]# cd accp
    8. [root@localhost accp]# vim index.html
    9. this is accp!
    10. [root@localhost accp]# vim /etc/hosts
    11. 20.0.0.61 www.pup.com www.accp.com
    12. [root@localhost conf]# nginx -t
    13. [root@localhost conf]# systemctl restart nginx.service

    浏览器访问www.pup.com

    浏览器访问www.accp.com

    基于IP配置nginx的虚拟主机

    1. [root@localhost conf]# ifconfig ens33:0 20.0.0.100/24
    2. [root@pup1 conf]# vim nginx.conf

    1. [root@localhost conf]# nginx -t
    2. [root@localhost conf]# systemctl restart nginx.service

    浏览器访问20.0.0.61

    浏览器访问20.0.0.100

    基于端口配置nginx的虚拟主机

    1. [root@localhost accp]# cd /usr/local/nginx/conf/
    2. [root@localhost conf]# vim nginx.conf

    1. [root@localhost conf]# nginx -t
    2. [root@localhost conf]# systemctl restart nginx

    浏览器访问20.0.0.61:8080

    浏览器访问20.0.0.100:8888

    基于授权的访问控制

    1. [root@localhost ~}# yum -y install httpd-tools
    2. [root@localhost ~]# chown nginx /usr/local/nginx/passwd.db
    3. [root@localhost ~]# chmod 400 /usr/local/nginx/passwd.db
    4. [root@localhost ~]# cd /usr/local/nginx/conf/
    5. [root@localhost conf]# vim nginx.conf

    1. [root@localhost conf]# nginx -t
    2. [root@localhost conf]# systemctl restart nginx

    浏览器访问20.0.0.61:8080

    基于客户端的访问控制

    [root@localhost conf]# vim nginx.conf
    

    1. [root@localhost conf]# nginx -t
    2. [root@localhost conf]# systemctl restart nginx

     20.0.0.10主机无法访问

    20.0.0.20主机可以访问

  • 相关阅读:
    深度!程序员生涯的垃圾时间(上)
    开源大数据管理平台选型
    JAVA反射(原理+使用)
    【LOJ#6718】九个太阳「弱」化版(循环卷积,任意模数NTT)
    QOS技术
    liteos开篇
    Mysql004:用户管理
    算法通关村——数字中的统计、溢出、进制转换处理模板
    subplot每张子图显示相同矢量
    基于猕猴感觉运动皮层Spike信号的运动解码分析不同运动参数对解码的影响
  • 原文地址:https://blog.csdn.net/pupcarrot/article/details/133777122