博文结构
Nginx介绍
Nginx的核心特色
Nginx平滑升级
修改Nginx版本信息
Nginx虚拟主机配置
nginx配置文件location选项的做用
配置https访问nginx
开启Nginx访问认证css
Nginx是一款轻量级的网页服务器、反向代理服务器以及电子邮件代理服务器。因它的稳定性、丰富的功能集、实例配置文件和低系统资源消耗而闻名。html
Nginx已经在俄罗斯最大的门户网站上运行,同时俄罗斯有超过20%的虚拟主机平台采用Nginx做为反向代理服务器;在国内,Nginx已经运行在淘宝、新浪、网易等多家网站使用Nginx做为Web服务器或反向代理服务器。前端
(1)跨平台:Nginx 能够在大多数 OS 编译运行,并且也有 Windows 的版本 (2)配置异常简单:很是容易上手 (3)非阻塞、高并发链接:官方测试可以支撑 5 万并发链接,在实际生产环境中跑到 2~3 万并发链接数。(这得益于 Nginx 使用了最新的 epoll 模型) (4)事件驱动:采用 epoll 模型,支持更大的并发链接 (5)Master/Worker 结构:一个 master 进程,生成一个或多个 worker 进程
(6)内存消耗小:处理大并发的请求内存消耗很是小。在 3 万并发链接下,开启的 10 个 Nginx 进程才消耗 150M 内存(15Mx10=150M) (7)内置的健康检查功能:若是 Nginx 代理的后端的某台 Web 服务器宕机了,不会影响 前端访问。 (8)节省带宽:支持 GZIP 压缩,能够添加浏览器本地缓存的 Header 头。 (9)稳定性高:用于反向代理,宕机的几率微乎其微
下载软件包nginx
[root@localhost ~]# yum -y install pcre-devel openssl-devel [root@localhost ~]# tar zxf nginx-1.14.0.tar.gz [root@localhost ~]# tar zxf nginx-1.2.4.tar.gz [root@localhost nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module && make && make install [root@localhost nginx-1.14.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ [root@localhost nginx-1.14.0]# nginx [root@localhost nginx-1.14.0]# useradd nginx -s /sbin/nologin -M [root@localhost nginx-1.2.4]# cd nginx-1.2.4/ [root@localhost nginx-1.2.4]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module && make [root@localhost nginx-1.2.4]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old [root@localhost nginx-1.2.4]# cp objs/nginx /usr/local/nginx/sbin/ [root@localhost nginx-1.2.4]# netstat -anpt | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0: LISTEN 17739/nginx: master [root@localhost nginx-1.2.4]# kill -USR2 17739 [root@localhost nginx-1.2.4]# nginx -s reload [root@localhost ~]# kill -QUIT 17739 ////平滑的关闭旧版的nginx进程 [root@localhost nginx-1.2.4]# nginx -V \\查看版本 nginx version: nginx/1.2.4 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module
QUIT 平滑关闭 HUP 平滑重启,从新加载配置文件 USR1 从新打开日志文件 USR2 平滑升级可执行程序 WINCH 平滑关闭工做进程
[root@localhost ~]# vim /usr/src/nginx-1.2.4/src/core//nginx.h ……………… //省略部份内容 #define nginx_version 1002004 #define NGINX_VERSION "8.8.8.8" //根据实际状况修改成本身想要的信息 #define NGINX_VER "xws/" NGINX_VERSION //同上,注意修改完的lzj [root@localhost ~]# vim /usr/src/nginx-1.2.4/src/http/ngx_http_header_filter_module.c ……………… //省略部份内容 static char ngx_http_server_string[] = "Server: xws" CRLF; //与上一个文件中修改的名称同样(lzj) static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF; [root@localhost ~]# vim /usr/src/nginx-1.2.4/src/http/ngx_http_special_response.c ……………… //省略部份内容 static u_char ngx_http_error_tail[] = "<hr><center>xws</center>" CRLF //注意与上两个文件中修改的lzj要一致 "</body>" CRLF "</html>" CRLF ; [root@localhost ~]# cd /usr/src/nginx-1.2.4/ [root@localhost nginx-1.2.4]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module && make [root@localhost ~]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak [root@localhost ~]# cp /usr/src/nginx-1.2.4/objs/nginx /usr/local/nginx/sbin/ [root@localhost ~]# nginx -s stop //中止nginx服务 [root@localhost ~]# nginx //开启nginx服务 [root@localhost ~]# curl -I 127.0.0.1 HTTP/1.1 200 OK Server: xws/8.8.8.8 //查看版本信息 Date: Sat, 30 Nov 2019 15:06:32 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Sat, 30 Nov 2019 14:42:09 GMT Connection: keep-alive Accept-Ranges: bytes
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf \\在http中加入如下 http { include mime.types; default_type application/octet-stream; server { \\从这里开始加 listen 80; server_name www.accp.com; location / { root /accp; index index.html index.htm; } } server { listen 80; server_name www.bdqn.com; location / { root /bdqn; index index.html index.htm; } } [root@localhost ~]# mkdir /bdqn [root@localhost ~]# mkdir /accp [root@localhost ~]# vim /bdqn/index.html www.bdqn.com [root@localhost ~]# vim /accp/index.html www.accp.com [root@localhost ~]# nginx -s reload [root@localhost ~]# vim /etc/hosts 192.168.148.130 www.bdqn.com 192.168.148.130 www.accp.com [root@localhost ~]# curl www.bdqn.com www.dbqn.com [root@localhost ~]# curl www.accp.com www.accp.com
root:实际访问的文件会被拼接URL的路径 alias:实际访问的文件路径不会拼接URL的路径
[root@localhost /]# vim /usr/local/nginx/conf/nginx.conf location ^~ /www { root /var/www/html; //当访问192.168.148.130/www/时,寻找/var/www/html下的www目录下的文件 index index.html index.htm; } [root@localhost /]# mkdir -p /var/www/html/www/ [root@localhost /]# vim /var/www/html/www/index.html www [root@localhost /]# nginx -s reload
访问以下:web
[root@localhost /]# vim /usr/local/nginx/conf/nginx.conf location ^~ /test { alias /var/www/html; \\寻找/var/www/html下的网页文件 index index.html index.htm; } [root@localhost /]# vim /var/www/html/index.html test [root@localhost /]# nginx -s reload
访问以下:vim
[root@localhost /]# vim /usr/local/nginx/conf/nginx.conf location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { root /webroot/res/; \\当访问以上内容结尾的页面就会去找//webroot/res路径下的文件 index index.html index.htm; } [root@localhost /]# mkdir /webroot/res -p [root@localhost /]# mv u=3485698722\,1702346544\&fm\=26\&gp\=0.jpg /webroot/res/a.png [root@localhost /]# ls /webroot/res a.png [root@localhost /]# nginx -s reload
[root@localhost /]# vim /usr/local/nginx/conf/nginx.conf location ~* .(gif|jpg|jpeg|png|css|js|ico)$ { \\这个加入上面的命令上面,要不容易报错 rewrite .(gif|jpg) /error.png; } [root@localhost /]# mv xxx.jfif /usr/local/nginx/html/error.png [root@localhost /]# ls /usr/local/nginx/html/ [root@localhost /]# nginx -s reload
开启nginx的https功能,须要编译是添加选项- -with-http_ ssl module后端
[root@localhost ca]# mkdir /usr/local/nginx/ca [root@localhost ca]# cd /usr/loc[root@localhost ca]# openssl genrsa -out ca.key 4096 al/nginx/ca/ \\一下内容随便填 Generating RSA private key, 4096 bit long modulus ......................................++ .......................++ e is 65537 (0x10001) [root@localhost ca]# openssl req -new -x509 -days 7304 -key ca.key -out ca.crt You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:aa State or Province Name (full name) []:cc Locality Name (eg, city) [Default City]:vv Organization Name (eg, company) [Default Company Ltd]:as Organizational Unit Name (eg, section) []:df Common Name (eg, your name or your server's hostname) []:ff Email Address []:asd [root@localhost ca]# ls ca.crt ca.key [root@localhost ca]# nginx -s stop [root@localhost ca]# nginx [root@localhost ca]# vim /usr/local/nginx/conf/nginx.conf server { \\原来的server中添加以下: listen 443 ssl; server_name localhost; ssl_certificate /usr/local/nginx/ca/ca.crt; \\证书存放的路径 ssl_certificate_key /usr/local/nginx/ca/ca.key; \\密钥存放路径 ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; access_log /usr/local/nginx/logs/https-access.log;
nginx使用http2版本,须要nginx版本在1. 10以上,安装时须要添加编译选项--with-http v2 module
http2.0版本只能在https上使用,修改上- -步配置文件listen 443 ssl http2;浏览器
[root@localhost nginx-1.14.0]# cd nginx-1.14.0/ [root@localhost nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module [root@localhost nginx-1.14.0]# make //开启nginx认证页面 须要使用htpasswd命令生成用户信息 [root@localhost /]# yum -y install httpd-tools [root@localhost ~]# htpasswd -c /usr/local/nginx/.passwd xws New password: Re-type new password: Adding password for user xws //用户认证信息存放路径是/usr/local/nginx/.passwd //若要向.passwd中添加第二个用户,须要省略“-c”选项,不然会覆盖以前的全部用户。 [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf ............... //省略部份内容 location / { root html; index index.html index.htm; auth_basic "请输入登陆帐号"; //添加提示语句 auth_basic_user_file /usr/local/nginx/.passwd; //认证信息存放路径 }