8244 发表于 2018-11-11 10:26:36

Linux Operation学习------Nginx

  1、Nginx
  是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP代理服务器;
  在连接高并发的情况下,优越性明显要高于apache。
  1.1认识
  Apache,Nginx,Lighttpd(适用于非Java程序写的页面)
  Tomcat,Jboss(适用于Java程序写的)
  用户概念:源码包安装的时候需要建立一个用户,该用户用来执行该服务(如果没有建立,系统默认使用nobody帐号),以防使用root造成不安全的后果,而yum源在装包的时候会自动为系统建立一个用户。
  配置文件:/usr/local/nginx/conf/nginx.conf
  日志文件:/usr/local/nginx/logs
  网页页面:/usr/local/nginx/html/index.html
  自定义网页页面:/usr/local/nginx/目录名称
  模块化安装需要的模块
  ./configure --with-模块名称 --with-模块名称
  1.2对原有的nginx添加模块(升级)
  1、删除原有的解包后的nginx的目录,重新解压
  2、cd到该目录下
  3、./configure --with-httpd_ssl_module
  4、make (把源码变成二进制程序,多了一个nginx执行程序)
  5、不能make install (会覆盖objs原有的文件)
  6、cpobjs/nginx/usr/local/nginx/sbin#升级只需要拷贝
  1.3部署
  1)使用源码包安装nginx软件包
  # yum –y install gcc pcre-devel openssl-devel   //安装常见依赖包 (gcc-c++)
  # useradd –s /sbin/nologin nginx
  # tar-xf   nginx-1.8.0.tar.gz
  # cdnginx-1.8.0
  # ./configure   \
  #> --prefix=/usr/local/nginx   \                  //指定安装路径
  #> --user=nginx   \                            //指定用户
  #> --group=nginx\                            //指定组
  #> --with-http_ssl_module                        //开启SSL加密功能
  .. ..
  make& make install
  2)nginx命令的用法 (可以建立软连接,执行方便)
  # /usr/local/nginx/sbin/nginx                  //启动服务
  # /usr/local/nginx/sbin/nginx -s stop            //关闭服务

  # /usr/local/nginx/sbin/nginx -s>  # /usr/local/nginx/sbin/nginx –V                //查看软件信息
  ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ (启动服务只需输入nginx)
  # netstat-anptu|grep nginx
  tcp      0      0 0.0.0.0:80      0.0.0.0:      LISTEN      10441/ngi
  2、用户认证
  2.1修改Nginx配置文件
  # vim /usr/local/nginx/conf/nginx.conf
  server {
  listen       80;               #相当于 :80
  server_namelocalhost;
  auth_basic "Input Password:";               #认证提示符
  auth_basic_user_file "/usr/local/nginx/pass";   #认证密码文件(文件不存在)
  location / {
  root   html;
  indexindex.html index.htm;
  }
  }
  一个server是一个网站
  2.2生成密码文件,创建用户及密码
  使用htpasswd命令创建账户文件,需要确保系统中已经安装了httpd-tools
  # yum -y installhttpd-tools
  # htpasswd -cm /usr/local/nginx/pass tom //创建密码文件(和配置文件内保持一致)
  New password:
  Re-type new password:
  Adding password for user tom
  # htpasswd -m /usr/local/nginx/pass   jerry
  //追加用户,不使用-c选项
  New password:
  Re-type new password:
  Adding password for user jerry
  # cat /usr/local/nginx/pass
  2.3重启Nginx服务

  # /usr/local/nginx/sbin/nginx –s>  tailf /usr/local/nginx/logs/#动态查看错误信息
  3、基于域名的虚拟主机
  3.1服务端:
  1)修改Nginx服务配置,添加相关虚拟主机配置如下
  # vim /usr/local/nginx/conf/nginx.conf
  server {
  listen       80;                                    //端口
  server_namewww.aa.com;                        //域名
  auth_basic "Input Password:";                               //认证提示符
  auth_basic_user_file "/usr/local/nginx/pass";               //认证密码文件
  location / {
  root   html;                                 //指定网站根路径
  indexindex.html index.htm;
  }
  }
  server {
  listen80;                            //端口
  server_namewww.bb.com;         //域名
  location / {
  root   www;                                 //指定网站根路径(需要创建目录名字为www)
  indexindex.html index.htm;
  }
  }
  在文件中使用ctrl+v并移动上下键可以选定字符,按x可以删除
  2)创建网站根目录及对应首页文件
  # mkdir /usr/local/nginx/www
  # echo "www" > /usr/local/nginx/www/index.html
  3)重启nginx服务

  # /usr/local/nginx/sbin/nginx –s>  3.2客户端:
  1)修改客户端主机192.168.4.100的/etc/hosts文件,进行域名解析
  /etc/hosts#本地域名解析文件在客户端上改 优先级高于DNS
  # vim /etc/hosts
  192.168.4.5    www.aa.com www.bb.com
  2)测试   firefox http://www.aa.comfirefox http://www.bb.com
  4、SSL虚拟主机
  加密算法:
  (1)对称加密
  (2)非对称加密
  (3)哈希值 md5sum +文件
  4.1生成私钥与证书
  # cd /usr/local/nginx/conf
  # openssl genrsa -out cert.key(或者openssl genrsa > cert.key)//生成私钥
  # openssl req -new -x509 -key cert.key -out cert.pem      //生成证书
  国家,省份,城市,公司,部门,主机名
  4.2修改Nginx配置文件,设置加密网站的虚拟主机
  # vim/usr/local/nginx/conf/nginx.conf
  server {
  listen       443 ssl;
  server_namewww.cc.com;
  ssl_certificate      cert.pem;      #证书名称和生成的保持一致
  ssl_certificate_keycert.key;       #私钥名称和生成的保持一致
  ssl_session_cache    shared:SSL:1m;
  ssl_session_timeout5m;
  ssl_ciphersHIGH:!aNULL:!MD5;
  ssl_prefer_server_cipherson;
  location / {
  root   html;      #此处修改需要在/usr/local/nginx/下新建名称一致的目录名
  indexindex.html index.htm;
  }
  }

  ngnix -s>  4.3客户端验证
  # vim /etc/hosts
  192.168.4.5    www.cc.comwww.aa.com   www.bb.com
  # firefox https://www.cc.com            //信任证书后可以访问
  5、Nginx反向代理
  负载均衡;检查后台情况(web高可用);
  5.1部署后端Web服务器
  yum-yinstallhttpd
  echo "192.168.2.100" > /var/www/html/index.html
  systemctl restart httpd
  5.2部署Nginx服务器
  由于配置文件在使用过程中,在/usr/local/nginx/conf下存在自带配置备份文件
  nginx.conf.default
  在实际工作中修改配置文件前对其进行先备份
  cp nginx.conf.default nginx.conf (对于本实验覆盖后重新做)
  修改/usr/local/nginx/conf/nginx.conf配置文件
  http {
  upstream webserver {               #定义一个web集群,取名webserver
  server 192.168.2.100:80;      #后台服务器
  server 192.168.2.200:80;
  }#该函数定义集群
  #可以添加weight权重:调用次数 ,
  #max_fails失败次数:允许最多连接后台web失败的次数
  #fail_timeout=10 超时时间:当失败后,10s后再询问后台的web是否正常(加down10s后不在询问)
  server {
  listen      80;
  server_namewww.tarena.com;
  location / {                  #匹配用户的地址栏
  proxy_pass http://webserver;   #调用集群
  roothtml;               #该路径不再寻找
  5.3重启服务

  /usr/local/nginx/sbin/nginx –s>  客户端轮询访问2个web
  5.4设置相同客户端访问相同Web服务器
  upstream webserver {            #定义一个web集群,取名webserver
  ip_hash;                         #客户端第一次访问之后,以后继续分配到这台web
  server 192.168.2.100:80;          #后台服务器
  server 192.168.2.200:80;
  }
  客户端访问只访问一个web
  1、动态网站
  LNMP(Linux,nginx,mariadb,php,python)
  1.1安装nginx
  1)解包 tar -xf
  配置 ./configure --prefix=/usr/local/nginx --with-http_ssl_module
  编译 make
  安装 make install
  2)mariadb 使用mysql命令
  mariadb-server 存放数据的地方 监听3306端口
  mariadb-devel依赖关系
  3)php【解释器】
  
  php-fpm监听9000端口 自动解释代码的服务
  php-mysql 扩展包,连接数据库
  4)启动服务以及查看端口状态
  /usr/local/nginx/sbin/nginx            #启动Nginx服务
  netstat -utnlp | grep :80
  systemctl start mariadb               #启动数据库服务
  netstat -utnlp | grep :3306或者 systemctl status mariadb
  systemctl start php-fpm            #启动php-fpm服务
  netstat -utnlp | grep :9000或者 systemctl status php-fpm
  1.2动静分离
  当用户访问网站时会根据location来匹配寻找的页面,没有匹配的就匹配 / 中的内容
  需要将php脚本放在/usr/local/nginx/html下,并修改下面的配置文件
  vim /usr/local/nginx/conf/nginx.conf
  location ~ .php$ {                         #匹配是否以.php结尾
  root         html;             #页面的位置
  fastcgi_pass   127.0.0.1:9000;#把找到的页面给了9000(php-fpm IP与端口)
  fastcgi_indexindex.php;
  include      fastcgi.conf;      #加载fastcgi.conf参数文件
  }
  日志查看:tailf /usr/local/nginx/logs/error.log
  ls/var/log/php-fpm/error.log
  测试:firefox http://192.168.4.5/1.php
  vim 1.php
  
  1.3 构建LNMP
  FastCGI :是一种常驻(long-live)型的CGI
  将CGI解释器进程保持在内存中,进行维护与调度
  配置文件路径:/etc/php-fpm.d/www.conf
  # vim /etc/php-fpm.d/www.conf#无须修改
  (3)创建PHP测试页面,连接MariaDB数据库:
  # vim /usr/local/nginx/html/test2.php
  
  (4)创建PHP测试页面,连接并查询MariaDB数据库
  # vim /usr/local/nginx/html/test3.php
  
  (5)客户端使用浏览器访问服务器PHP首页文档,检验是否成功
  # firefox http://192.168.4.5/test2.php
  # firefox http://192.168.4.5/test3.php
  2、地址重写
  获得一个来访的URL请求,然后改写成服务器可以处理的另一个URL的过程
  rewrite旧链接(支持正则)新链接[选项];
  选项:last不再读其他rewrite;break 不再读其他语句,结束;
  redirect 让地址栏变化,用户能看到url的改变(临时);permament让地址栏变化(永久)
  1)修改配置文件(访问a.html重定向到b.html)
  # vim /usr/local/nginx/conf/nginx.conf
  .. ..
  server {
  listen       80;
  server_namelocalhost;
  location / {
  root   html;
  indexindex.html index.htm;
  rewrite /a.html/b.html;             #访问a.html的内容会跳到b.html (可以缩短URL)
  }
  }
  echo www.a.com > html/a.html
  echo www.b.com > html/b.html

  # /usr/local/nginx/sbin/nginx-s >  # firefoxhttp://192.168.4.5/a.html
  2)修改配置文件(访问192.168.4.5的请求重定向至www.a.com)
  # vim /usr/local/nginx/conf/nginx.conf
  server {
  listen       80;
  server_namelocalhost;
  rewrite ^/http://www.a.com/;   #在进入网站之前匹配到以 / 开头的都跳转到该网站
  location / {
  root   html;
  indexindex.html index.htm;
  }
  }

  # /usr/local/nginx/sbin/nginx-s >  3)修改配置文件(访问192.168.4.5/下面子页面,重定向至www.tmooc.cn/下相同的页面)
  # vim /usr/local/nginx/conf/nginx.conf
  server {
  listen       80;
  server_namelocalhost;
  rewrite ^/(.) http://www.a.com/$1; #访问子网站都跳转到现有对应的,一个()对应一个$
  location / {
  root   html;
  indexindex.html index.htm;
  }
  }

  # /usr/local/nginx/sbin/nginx-s >  访问http://192.168.4.5/web/login_new.html跳转到
  http://www.tmooc.cn/web/login_new.html
  4)实现curl和火狐访问相同连接返回的页面不同
  .. ..
  server {
  listen       80;
  server_namelocalhost;
  location / {
  root   html;
  indexindex.html index.htm;
  }
  if($http_user_agent ~ url) {               //识别客户端curl浏览器 不分大小写
  rewrite ^(.)$ /curl/$1 break;
  }
  }
  # echo "firefox" > /usr/local/nginx/html/test.html
  # mkdir-p/usr/local/nginx/html/curl/
  # echo "curl" > /usr/local/nginx/html/curl/test.html

  # /usr/local/nginx/sbin/nginx-s >  # firefoxhttp://192.168.4.5/test.html   #出现firefox页面
  # curl   http://192.168.4.5/test.html    #返回curl的信息
  实际应用:区分是PC页面或者手机页面或者其他
  $http_user_agent 用户请求的包含用户的信息的变量
  tailf   /usr/local/nginx/logs/access.log         #访问信息日志
  192.168.4.254 - - "GET /test.html HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0"
  192.168.4.254 - - "GET /favicon.ico HTTP/1.1" 404 168 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0"
  访问服务器的用户IP - 用户名时间访问什么   用什么系统访问用什么浏览器访问
  # curl -A firefoxhttp://192.168.4.5/test.html
  Firefox   #并不显示curl
  #curl伪装成firefox进行对服务器访问,服务器访问日志显示伪装后的信息
  1、Nginx常见问题处理
  1.1不显示Nginx软件版本号
  server_tokens off/on;#服务器版本号信息
  1.2并发量
  ab –n 2000 –c 2000 http://192.168.4.5/   -c 并发量 -n 请求数
  如何增大并发量的容量
  1)通过nginx配置文件修改
  worker_processes2;                #与CPU核心数量一致
  events {
  worker_connections 65535;          #每个worker最大并发连接数
  use epoll;
  }
  2)通过Linux系统内核
  ulimit -a#查看最大所有限制
  ulimit –Hn 100000   #临时有效
  ulimit –Sn 100000   #临时有效
  -S软限制(用户可以修改) n(最大文件数量)
  -H硬限制(用户不可以修改) n(最大文件数量)
  ss -anptu | grep nginx#实时查看并发量有多少人访问 (| wc-l)
  永久设置:
  # vim /etc/security/limits.conf
        

[*]soft   nofile      100000
[*]hard    nofile      100000  只能是soft或者hard
  1.3如何解决客户端访问头部信息过长的问题
  报错信息414(缓存不够)414 Request-URI Too Large
  # vim /usr/local/nginx/conf/nginx.conf
  http {
  server_tokens off;                      //不显示nginx版本号信息
  client_header_buffer_size    1k;      //默认请求包头信息的缓存
  large_client_header_buffers4 4k;      //请求包头部信息的缓存个数与容量
  ......
  1.4开启gzip压缩功能,提高数据传输效率
  # vim /usr/local/nginx/conf/nginx.conf
  gzip on;               #开启压缩功能
  gzip_min_length 1000;#字节限定(小文件不压缩)
  gzip_comp_level 4;   #压缩比率1-9
  gzip_types text/plain   #对什么格式的文件压缩
  参考:/usr/local/nginx/conf/mime.types将格式左边的写到gzip_types后
  对mp4,mp3,jpg不能压缩,多媒体文件基本都是压缩格式
  1.5如何让客户端浏览器缓存数据
  about:cache #查看浏览器缓存
  针对不变的数据进行缓存
  # vim /usr/local/nginx/conf/nginx.conf
  server {
  listen       80;
  server_namewww.tarena.com;
  location / {
  root   html;
  indexindex.html index.htm;
  }
  location ~* .(jpg|jpeg|gif|png|css|js|ico|xml)$ {
  expires      30d;            #定义客户端缓存时间为30天
  }
  }
  1.6如何自定义返回给客户端的404错误页面
  server {
  ....
  charset utf-8;
  error_page   404/40x.html;    //自定义错误页面
  location = /40x.html {
  root   html;
  }
  }

  /usr/local/nginx/conf/sbin/nginx -s>
页: [1]
查看完整版本: Linux Operation学习------Nginx