服务器运维-Nginx

  • 搭建Nginx服务器

  • 在主机上安装部署Nginx服务,并可以将Nginx服务器要求编译时启用如下功能:
    • 支持SSL加密功能
    • 设置Nginx账户及组名称均为nginx
    • Nginx服务器升级到最高版本
  • 然后客户端访问页面验证Nginx Web服务器:
    • 使用浏览器访问
    • 使用curl访问
    1. 安装步骤
  • nginx网址:http://nginx.org/en/download.html
  • [root@localhost ~]# yum -y install gcc pcre-devel openssl-devel
    • gcc:c语言编译器
    • pcre-devel:正则
    • openssl-devel:加密
  • [root@localhost ~]# useradd -s /sbin/nologin nginx
  • 添加nginx用户名,并制定解释器
  • [root@localhost nginx-1.10.3]# tar -xf nginx-1.10.3.tar.gz
  • 进入安装包的目录解包
  • [[root@localhost nginx-1.10.3]# ./configure \

–prefix=/usr/local/nginx
–user=nginx
–group=nginx
–with-http_ssl_module

  • 进入解压缩的目录后配置安装
  • [root@localhost nginx-1.10.3]# make && make install
    1. 操作命令
  • [root@localhost ~]# /usr/local/nginx/sbin/nginx
  • 启动服务
  • [root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop
  • 关闭服务
  • [root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
  • 重新加载配置文件
  • [root@localhost ~]# /usr/local/nginx/sbin/nginx -V
  • 查看版本及安装时的详细信息
  • [root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /sbin/
  • 创建软链接,方便后期使用
  • netstat命令可以查看系统中启动端口信息,该命令常用选项如下:
  • -a:显示所有端口信息
  • -n:以数字格式显示端口号
  • -t:显示TCP连接端口
  • -u:显示UDP连接端口
  • -l:显示服务器正在监听的端口信息,如httpd启动后,会一直监听80端口
  • -p:显示监听端口的服务名称
  • [root@localhost ~]# netstat -antulp
  • [root@localhost ~]# netstat -antulp | awk ‘/nginx/’
  • 顺便复习一下awk
  • 升级Nginx服务器

    1. 编译新版本nginx软件
  • [root@localhost opt]# tar -zxvf nginx-1.12.2.tar.gz
  • [root@localhost opt]# cd nginx-1.12.2/
  • [root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx \

–group=nginx
–with-http_ssl_module

  • 配置,并自动在/opt/nginx-1.12.2/下生成objs目录

  • [root@localhost nginx-1.12.2]# make

  • 编译源代码,在/opt/nginx-1.12.2/objs下生成nginx可执行程序

  • [root@localhost nginx-1.12.2]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old

  • 将原来旧nginx改名

  • [root@localhost nginx-1.12.2]# cp /opt/nginx-1.12.2/objs/nginx /usr/local/nginx/sbin/nginx

  • 将新生成的nginx拷贝过去

  • [root@localhost nginx-1.12.2]# make upgrade

  • 重启服务

  • [root@localhost nginx-1.12.2]# killall nginx
    [root@localhost nginx-1.12.2]# /usr/local/nginx/sbin/nginx

  • 如果重启提示错误,可以先杀掉进程,再启动

  • 用户认证

  • 要求:

    1. 访问web页面需要进行用户认证
    1. 用户名为tom,密码为:123456
  • 方案:

  • 通过Nginx实现web页面的认知,需要修改Nginx配置文件,在配置文件中添加auth语句实现用户认证。最后使用htpasswd命令创建用户及密码即可。

  • 步骤:

    1. 修改Nginx配置文件/usr/local/nginx/conf/nginx.conf
  • 找到server,在server_name下添加两条语句

  • 35 server {
    36 listen 80;
    37 server_name localhost;
    38 auth_basic “Input Password:”;
    39 auth_basic_user_file “/usr/local/nginx/pass”;

    40
    41 #charset koi8-r;
    42
    43 #access_log logs/host.access.log main;
    44
    45 location / {
    46 root html;
    47 index index.html index.htm;
    48 }
    auth_basic "Input Password"是认证提示符信息
    auth_basic_user_file "/usr/local/nginx/pass"是认证的密码文件,需自己创建

    1. 生成密码文件,创建用户及密码
  • 使用htpsswd命令创建账户文件,需要确保系统中已经安装了httpd-tools

  • [root@localhost conf]# yum -y install httpd-tools

  • 使用htpasswd创建密码文件

  • [root@localhost conf]# htpasswd -c /usr/local/nginx/pass tom
    New password:
    Re-type new password:
    Adding password for user tom

  • 追加用户,不使用-c选项

  • [root@localhost conf]# htpasswd /usr/local/nginx/pass jerry

  • 重新加载配置文件

  • [root@localhost conf]# nginx -s reload

  • 基于域名的虚拟主机

  • 目标:

    1. 实现两个基于域名的虚拟主机,域名分别为www.a.com和www.b.com
    1. 对域名为www.a.com的站点进行用户认证,用户名称为tom,密码为123456
  • 方案:

  • 修改Nginx配置文件,添加server容器实现虚拟主机功能;对于需要进行用户认证的虚拟主机添加auth认证语句。

  • 虚拟主机一般可分为:基于域名、基于IP和基于端口的虚拟主机。

  • 步骤

  • 沿用上面的配置,将localhost该为www.a.com

  • 35 server {
    36 listen 80;
    37 server_name www.a.com;
    38 auth_basic “Input Password:”;
    39 auth_basic_user_file “/usr/local/nginx/pass”;
    40
    41 #charset koi8-r;
    42
    43 #access_log logs/host.access.log main;
    44
    45 location / {
    46 root html;
    47 index index.html index.htm;
    48 }

  • www.b.com使用后面的示例

  • 使用:86,95s/#//反注释

  • 86 server {
    87 listen 80;
    88 server_name www.b.com;
    89
    90 location / {
    91 root www;
    92 index index.html index.htm;
    93 }
    94 }
    配置结束

  • 创建www目录,和index.html文件

  • [root@localhost nginx]# cp -r html www

  • [root@localhost nginx]# vim www/index.html

  • reload配置

  • [root@localhost nginx]# nginx -s reload

  • SSL虚拟主机

  • 目标:

  • 域名为www.c.com

  • 该站点通过https访问

  • 通过私钥、证书对该站点所有数据加密

  • 方案:

  • 源码安装Nginx时必须使用–with_ssl_module参数,启用加密模块,对需要进行SSL加密处理的站点添加ssl相关指令(设置网站需要的私钥和证书)

  • 加密算法一般分为对称算法、非对称算法、信息摘要

  • 对称算法有:AES、DES,主要应用在淡季数据加密

  • 非对称算法有:RSA、DSA,主要应用在网络数据加密

  • 信息摘要:MD5、sha256,主要应用在数据完整性校验

  • 步骤:

    1. 配置SSL虚拟主机
  • [root@localhost conf]# cd /usr/local/nginx/conf/

  • 进入conf目录

  • [root@localhost conf]# cat cert.key

  • 生成私钥

  • [root@localhost conf]# openssl req -new -x509 -key cert.key > cert.pem

  • 生成证书:req请求、new新的、x509格式、key秘钥

  • 打开nginx.conf,找到最后的模板,去掉注释

  • server {
    listen 443 ssl;
    server_name www.c.com;

      ssl_certificate      cert.pem;
      ssl_certificate_key  cert.key;
    
      ssl_session_cache    shared:SSL:1m;
      ssl_session_timeout  5m;
    
      ssl_ciphers  HIGH:!aNULL:!MD5;
      ssl_prefer_server_ciphers  on;
    
      location / {
          root   html;
          index  index.html index.htm;
      }
    

    }

}

  • LNMP

  • 需要的软件:
    • nginx
    • mariadb(数据库客户端)、mariadb-server(数据库服务器)、mariadb-devel(其他客户端软件的依赖包)
    • php(解释器)、php-fpm(进程管理器服务)、php-mysql(php的数据库扩展包)
  • 步骤:
  • 1.安装软件
  • 使用yum安装基础依赖包
  • [root@localhost conf]# yum -y install mariadb mariadb-server mariadb-devel
  • [root@localhost conf]# yum -y install php php-fpm php-mysql
  • 启动nginx
  • [root@localhost conf]# nginx -s reload
  • 启动mysql服务
  • [root@localhost conf]# systemctl start mariadb
    [root@localhost conf]# systemctl enable mariadb
    [root@localhost conf]# systemctl status mariadb
  • 启动php-fpm服务
  • [root@localhost conf]# systemctl start php-fpm
    [root@localhost conf]# systemctl enable php-fpm
    [root@localhost conf]# systemctl status php-fpm
  • 查询端口
  • [root@localhost conf]# netstat -antulp | grep 80
    tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 773/nginx: master p
    [root@localhost conf]# netstat -antulp | grep 3306
    tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 15770/mysqld
    [root@localhost conf]# netstat -antulp | grep 9000
    tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 16129/php-fpm: mast
  • 查看php-fpm配置文件(实验中不需要修改该文件)
  • [root@localhost conf]# awk ‘/^[^;]/{print}’ /etc/php-fpm.d/www.conf
    [www]
    listen = 127.0.0.1:9000 //php端口号

listen.allowed_clients = 127.0.0.1
user = apache
group = apache
pm = dynamic
pm.max_children = 50 //最大进程数
pm.start_servers = 5 //最小进程数
pm.min_spare_servers = 5 //最少空闲进程
pm.max_spare_servers = 35 //最大空闲进程

slowlog = /var/log/php-fpm/www-slow.log

php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session

  • 修改nginx配置文件并启动服务
  • [root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
  • 67 location ~ .php$ {
    68 root html;
    69 fastcgi_pass 127.0.0.1:9000;
    70 fastcgi_index index.php;
    71 #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    72 #include fastcgi_params;
    73 include fastcgi.conf;
    74 }
  • 重启nginx
  • [root@localhost conf]# nginx -s reload
  • 在html目录下创建test.php,测试成功
  • <?
  • $i=33
  • echo $i
  • ?>
    1. LNMP常见问题
  • Nginx的默认访问日志文件为/usr/local/nginx/logs/access.log
  • Nginx的默认错误日志文件为/usr/local/nginx/logs/error.log
  • PHP默认错误日志文件为/var/log/php-fpm/www-error.log
  • 如果动态网站访问失败,可以参考错误日志,查找错误信息
  • 地址重写

  • 修改配置文件(访问a.html重定向到b.html)
    1. 修改Nginx服务配置:
  • 35 server {
    36 listen 80;
    37 server_name www.a.com;
    38 rewrite /a.html /b.html;
  • 生成b.html
  • [root@localhost conf]# echo “bbb” > /usr/local/nginx/html/b.html
    1. 访问a.html重定向到b.html(跳转地址栏)
  • 修改Nginx服务配置
  • 35 server {
    36 listen 80;
    37 server_name www.a.com;
    38 rewrite /a.html /b.html redirect;
  • 重启服务
  • [root@localhost ~]# nginx -s reload
    1. 修改配置文件(访问10.211.55.21的请求重定向至www.tmooc.cn)
  • 修改Nginx服务配置
  • 35 server {
    36 listen 80;
    37 server_name www.a.com;
    38 #rewrite /a.html /b.html redirect;
    39 rewrite ^/ http://www.tmooc.cn;
  • 重启服务
  • [root@localhost ~]# nginx -s reload
    1. 修改配置文件(访问10.211.55.21/下面子页面,重定向至www.tmooc.cn/下相同的页面)
  • 修改Nginx服务配置
  • 35 server {
    36 listen 80;
    37 server_name www.a.com;
    38 #rewrite /a.html /b.html redirect;
    39 rewrite ^/(.*) http://www.tmooc.cn/$1;
    ()在正则里表示保留,\1表示保留的内容,在配置文件中使用$1
  • 重启服务
  • [root@localhost ~]# nginx -s reload
    1. 修改配置文件(实现curl和火狐访问相同链接返回页面不同)
  • 创建页面目录以及对应的页面
  • [root@localhost ~]# echo “I am Normal page” > /usr/local/nginx/html/test.html
    [root@localhost ~]# mkdir -p /usr/local/nginx/html/firefox
    [root@localhost ~]# echo “firefox” > /usr/local/nginx/html/firefox/test.html
  • 修改Nginx服务配置
  • 35 server {
    36 listen 80;
    37 server_name www.a.com;
    38 if ($http_user_agent ~* firefox){
    39 rewrite ^/(.*) /firefox/$1;
    40 }
    ~表示正则匹配,*表示不区分大小写
  • 重启服务
  • [root@localhost ~]# nginx -s reload
  • Nginx反向代理

  • 配置Nginx服务器,添加服务器池,实现反向代理功能
    1. 修改/usr/local/nginx/conf/nginx.conf配置文件
  • 35 upstream webserver{ //使用upstream定义后端服务器集群,集群名称任意(如webserver)
    36 server 192.168.2.100:80; //使用server定义集群中的具体服务器和端口
    37 server 192.168.2.200;
    38 }
    39 server {
    40 listen 80;
    41 server_name www.a.com;
    54 location / {
    55 proxy_pass http://webserver; //代理转发给webserver集群
    56 root html;
    57 index index.html index.htm;
    58 }
    1. 配置upstream服务器集群池属性
  • 设置失败次数,超时时间,权重
  • weight可以是设置后台服务器的权重,max_fails可以设置后台服务器的失败次数,fail_timeout可以设置后台服务器的失败超时时间。
  • 35 upstream webserver{
    36 server 192.168.2.100:80 weight=3; //100的权重更改为3
    37 server 192.168.2.200;
  • max_fails最大失败次数,30秒内不再被访问,down标记服务器已关机,不参与集群调度
    35 upstream webserver{
    36 server 192.168.2.100:80 max_fails=1 fail_timeout=30;
    37 server 192.168.2.200 down;
    38 }
    1. 配置upstream服务器集群的调度算法
  • 设置相同客户端访问相同web服务器(ip_hash)
  • 35 upstream webserver{
    36 ip_hash;
    37 server 192.168.2.100:80 max_fails=1 fail_timeout=30;
    38 server 192.168.2.200;
    39 }
  • Nginx的TCP/UDP调度器
  • 问题:
  • 使用Nginx实现TCP/UDP调度器功能,实现如下功能:
  • 后端SSH服务器两台
  • Nginx编译安装时需要使用–with-stream,开启ngx_stream_core_module模块
  • Nginx采用轮询的方式调用后端SSH服务器
  • 步骤:
    1. 部署支持4层TCP/UDP代理的Nginx服务器
  • 编译安装必须要使用–with-stream参数开启4层代理模块
  • [root@localhost nginx]# nginx -s stop
    [root@localhost nginx]# rm -rf /usr/local/nginx/
    干掉老的nginx
  • [root@localhost nginx-1.12.2]# ./configure
    > --with-http_ssl_module
    > --with-stream
    重新编译安装
    [root@localhost nginx-1.12.2]# make && make install
    [root@localhost nginx-1.12.2]# nginx -V
    nginx version: nginx/1.12.2
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
    built with OpenSSL 1.0.2k-fips 26 Jan 2017
    TLS SNI support enabled
    configure arguments: --with-http_ssl_module --with-stream
  • 配置Nginx服务器,添加服务器池,实现TPC/UDP反向代理功能
  • 修改/usr/local/nginx/conf/nginx.conf配置文件
  • 16 stream{
    17 upstream sshend{
    18 server 192.168.2.100:22;
    19 server 192.168.2.200:22;
    20 }
    21 server{
    22 listen 12345;
    23 proxy_pass sshend;
    24 }
    25 }
  • 启动服务
  • [root@localhost nginx-1.12.2]# nginx
  • Nginx常见问题处理

    1. 自定义报错页面
  • 修改/usr/local/nginx/conf/nginx.conf配置文件
  • 59 charset utf-8;
    60 error_page 404 /404.html;
    设置支持中文,反注释error行,自定义错误页面
  • [root@localhost nginx-1.12.2]# echo “Ooops,NO NO no page…” > /usr/local/nginx/html/404.html
  • [root@localhost nginx-1.12.2]# nginx -s reload
    1. 如何查看服务器状态信息(非常重要的功能)
  • 编译安装时使用–with-http_sub_status_module开启状态页面模块
  • [root@localhost nginx-1.12.2]# ./configure
    > --with-http_ssl_module \ //开启加密功能
    > --with-stream \ //开启TCP/UDP代理功能
    > --with-http_stub_status_module //开启status状态
  • [root@localhost nginx-1.12.2]# make
  • [root@localhost nginx-1.12.2]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
  • [root@localhost nginx-1.12.2]# cp objs/nginx /usr/local/nginx/sbin/nginx
  • [root@localhost nginx-1.12.2]# nginx -s reload
  • 平滑升级
  • 更改/usr/local/nginx/conf/nginx.conf配置文件
  • 54 location /status{
    55 stub_status on;
    56 #allow IP;
    57 #deny IP;
    58 }
  • 用浏览器去访问
  • Active connections: 1
    server accepts handled requests
    1 1 1
    Reading: 0 Writing: 1 Waiting: 0
    active connections:当前活动的连接数量
    accepts:已经接受客户端的连接总数量
    handled: 已经处理客户端的连接数量
    requests:客户端发送的请求数量
    reading:当前服务器正在读取客户端请求的数量
    1. 优化并发量
  • 优化前使用ab高并发测试
  • [root@pc207 ~]# ab -n 2000 -c 2000 http://10.211.55.21/
  • -c:client -n:number、、模拟2000个人一共访问2000次,每人1次。
  • Benchmarking 10.211.55.21 (be patient)
    socket: Too many open files (24)
    提示打开文件数量过多
  • 修改Nginx配置文件,增加并发量
  • 3 worker_processes 2; // 将cpu数量该为实际CPU数量
    • 12 events {
      13 worker_connections 65536;
      14 }
      单核的最大连接数,一般达不到这个值
    • 重新加载
    • Benchmarking 10.211.55.21 (be patient)
      socket: Too many open files (24)
      发现还是被限制,是因为系统对每个用户的限制1024
    • [root@localhost nginx-1.12.2]# ulimit -a
    • ulimit -a == user limit --all
    • open files (-n) 1024
    • [root@localhost nginx-1.12.2]# ulimit -Hn 100000
    • Hn是硬限制,实际最高的值(临时规则)
    • [root@localhost nginx-1.12.2]# ulimit -Sn 100000
    • Sn是软限制,告警阈值(临时规则)
    • 可以编辑/etc/security/limits.conf
    • * soft nofile 10000
    • * hard nofile 10000
    • 用户或组 硬限制或软限制 需要限制的项目 限制的数量
    • [root@localhost nginx-1.12.2]# ab -c 2000 -n 2000 http://10.211.55.21/
    • Percentage of the requests served within a certain time (ms)
      50% 117
      66% 120
      75% 124
      80% 127
      90% 130
      95% 131
      98% 132
      99% 132
      100% 152 (longest request)
    • ab的最大测试值是20000
    1. 数据包头优化
    • 配置/usr/local/nginx/conf/nginx.conf
    • 17 http {
      18 client_header_buffer_size 1k; //默认设置为1k
      19 large_client_header_buffers 4 4k; //最大设置为4个4k共16k
      [root@localhost nginx-1.12.2]# nginx -s reload
    1. 浏览器本地缓存静态数据
    • 编辑/usr/local/nginx/conf/nginx.conf
    • 45 location ~*.(jbp|jpeg|gif|png|ico)$ {
      46 expires 30d;
      47 }
    1. 日志切割
    • [root@localhost nginx-1.12.2]# ls /usr/local/nginx/logs/
      access.log error.log nginx.pid
    • [root@localhost nginx-1.12.2]# wc -l /usr/local/nginx/logs/access.log
      4252 /usr/local/nginx/logs/access.log
      [root@localhost nginx-1.12.2]# ll -h /usr/local/nginx/logs/access.log
      -rw-r–r--. 1 root root 389K 9月 7 00:47 /usr/local/nginx/logs/access.log
      access.log文件会越来越大
    • 手工切割
    • [root@localhost nginx-1.12.2]# cd /usr/local/nginx/logs/
      [root@localhost logs]# ls
      access.log error.log nginx.pid
      [root@localhost logs]# mv access.log access2.log
      [root@localhost logs]# kill -USR1 `cat nginx.pid`
      nginx.pid里是nginx的进程号
      [root@localhost logs]# ls
      access2.log access.log error.log nginx.pid
    • 编写脚本自动完成
    • vim /usr/local/nginx/logbak.sh
    • #!/bin/bash
      date=date +%Y%m%d
      logpath=/usr/local/nginx/logs
      mv $logpath/access.log l o g p a t h / a c c e s s − logpath/access- logpath/accessdate.log
      mv $logpath/error.log l o g p a t h / e r r o r − logpath/error- logpath/errordate.log
      kill -USR1 cat $logpath/nginx.pid
    • 加入计划
    • crontab -e
    • 03 03 * * 5 /usr/local/nginx/logbak.sh
    1. 对页面进行压缩处理
    • 修改/usr/local/nginx/conf/nginx.conf
    • http{
    • 18 gzip on; //开启压缩
      19 gzip_min_length 1000; //小文件不压缩,小于1000字节不压缩
      20 gzip_comp_level 4; //压缩比率(1-9)
      21 gzip_types text/plain text/css application/json application; //对特定文件压缩,参考mime.tpye
    1. 服务器内存缓存
    • 如果需要处理大量静态文件,可以将文件缓存在内存,下次访问会更快。
    • 修改/usr/local/nginx/conf/nginx.conf
    • http {
    • 17 http {
      18 open_file_cache max=2000 inactive=20s;
      //设置服务器最大缓存2000个文件句柄,关闭20秒内不活动的缓存
      19 open_file_cache_valid 60s;
      //文件句柄的有效时间60秒,60秒后过期
      20 open_file_cache_min_uses 5;
      //只有访问次数超过5次会被缓存
      21 open_file_cache_errors off;
      //缓存错误不报错
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值