Nginx 实现web服务 虚拟主机 动静分离 小节3

205 阅读4分钟

@[TOC](Nginx 实现web服务 虚拟主机 动静分离 小节3)

前提

名称环境IP/GW安装方式
nginxCentos737.7/GW37.2YUM
nginx10Centos737.10/GW37.2编译

nginx配置

http协议的相关配置:

http {
   ... ...
   server {
          ...
          server_name
          root
          location [OPERATOR] /uri/ {
                ...
          } 
   }
   server {
          ...
   } 
}

ngx_http_core_module(参考下面示例)

  • 在响应报文中将指定的文件扩展名映射至MIME对应的类型
include    /etc/nginx/mime.types;
default_type    application/octet-stream; 除上面指定的类型外,就为默认的
MIME类型,浏览器一般会提示下载
types {
   text/html html;
   image/gif gif;
   image/jpeg jpg;
}
  • MIME参考文档:

developer.mozilla.org/zh-CN/docs/…

示例1. 默认类型会下载

[root@nginx ~]# vim /etc/nginx/nginx.conf
...
    include             /etc/nginx/mime.types;   <--默认
    default_type        application/octet-stream;   <--默认类型
...


[root@nginx ~]# cd /usr/share/nginx/html
#创建两个文件'.xyz'和'.php'结尾
[root@nginx html]# cp /etc/fstab test.xyz
[root@nginx html]# vim test.php 
<?php
phpinfo();
?>

[root@nginx html]# curl -I http://192.168.37.7/test.xyz
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Sat, 06 Aug 2022 05:23:14 GMT
Content-Type: application/octet-stream    <--默认类型
Content-Length: 595
Last-Modified: Sat, 06 Aug 2022 05:05:24 GMT
Connection: keep-alive
ETag: "62edf694-253"
Accept-Ranges: bytes

#启动服务
[root@nginx html]# nginx


浏览器打开时会默认下载
http://192.168.37.7/test.xyz
http://192.168.37.7/test.php

示例2. 纯文本格式

[root@nginx ~]# vim /etc/nginx/nginx.conf
...
    include             /etc/nginx/mime.types;
#    default_type        application/octet-stream;
...

#启动服务
[root@nginx html]# nginx

[root@nginx html]# curl -I http://192.168.37.7/test.xyz
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Sat, 06 Aug 2022 05:45:41 GMT
Content-Type: text/plain    <--文本类型
Content-Length: 595
Last-Modified: Sat, 06 Aug 2022 05:05:24 GMT
Connection: keep-alive
ETag: "62edf694-253"
Accept-Ranges: bytes

浏览器打开

图片.png 图片.png

示例3. 指定某种类型

[root@nginx ~]# vim /etc/nginx/nginx.conf
···
    include             /etc/nginx/mime.types;
#   default_type        application/octet-stream;
    default_type        test/html;    <--指定某种类型
···
[root@nginx ~]# nginx 
[root@nginx ~]# curl -I http://192.168.37.7/test.xyz
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Sat, 06 Aug 2022 06:00:16 GMT
Content-Type: test/html    <--指定某种类型
Content-Length: 595
Last-Modified: Sat, 06 Aug 2022 05:05:24 GMT
Connection: keep-alive
ETag: "62edf694-253"
Accept-Ranges: bytes

浏览器打开

图片.png 图片.png

ngx_http_core_module

  1. tcp_nodelay on | off;

在keepalived模式下的连接是否启用TCP_NODELAY选项,即Nagle算法

当为off时,延迟发送,每发送一个包就需要确认ACK,才发送下一个包

默认On时,不延迟发送,多个包才确认一次

可用于:http, server, location

  1. tcp_nopush on | off ;

在开启sendfile,on时合并响应头和数据体在一个包中一起发送

  1. sendfile on | off;

是否启用sendfile功能,在内核中封装报文直接发送,默认Off

  1. charset charset | off;

是否在响应报文中的Content-Type显示指定的字符集,默认off不显示

  1. server_tokens on | off | build | string;

是否在响应报文的Server首部显示nginx版本

示例: 显示指定的字符集

[root@nginx ~]# vim /etc/nginx/conf.d/test.conf
charset utf-8;   <--默认字符集设置


[root@nginx ~]# nginx

#显示字符集'utf-8'
[root@nginx ~]# curl -I http://192.168.37.7/
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Sat, 06 Aug 2022 06:29:12 GMT
Content-Type: text/html; charset=utf-8   <--显示字符集'utf-8'
Content-Length: 13
Last-Modified: Fri, 05 Aug 2022 18:00:12 GMT
Connection: keep-alive
ETag: "62ed5aac-d"
Accept-Ranges: bytes

示例: 隐藏nginx版本

[root@nginx ~]# vim /etc/nginx/conf.d/test.conf
···
charset utf-8;
server_tokens off;    <--隐藏nginx版本
···

#启动服务
[root@nginx ~]# nginx

[root@nginx ~]# curl -I http://192.168.37.7/
HTTP/1.1 200 OK
Server: nginx    <--
Date: Sat, 06 Aug 2022 06:35:09 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 13
Last-Modified: Fri, 05 Aug 2022 18:00:12 GMT
Connection: keep-alive
ETag: "62ed5aac-d"
Accept-Ranges: bytes

自定义nginx版本信息

  1. 如果想自定义响应报文的nginx版本信息,需要修改源码文件,重新编译
  2. 如果server_tokens on,修改 src/core/nginx.h 修改第13-14行,如下示例

#define NGINX_VERSION "1.68.9"

#define NGINX_VER "wanginx/" NGINX_VERSION

  1. 如果server_tokens off,修改 src/http/ngx_http_header_filter_module.c第49行,如下示例:

static char ngx_http_server_string[] = "Server: nginx" CRLF;

把其中的nginx改为自己想要的文字即可,如:wanginx

示例:

[root@nginx10 ~]# cd /usr/local/src/nginx-1.16.0/src/core/
[root@nginx10 core]# vim nginx.h 
...
#define NGINX_VERSION      "1.99.0"    <---
#define NGINX_VER          "Sunginx/" NGINX_VERSION    <--
...

[root@nginx10 core]# cd ..
[root@nginx10 src]# vim http/ngx_http_header_filter_module.c 
...
static u_char ngx_http_server_string[] = "Server: Sunginx" CRLF;    <--
static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF;
...

[root@nginx10 core]# cd /usr/local/src/nginx-1.16.0
#因为改了源码、所以要重新编译
[root@nginx10 nginx-1.16.0]# ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@nginx10 nginx-1.16.0]# make && make install


[root@nginx10 nginx-1.16.0]# nginx

[root@nginx10 ~]# curl -I http://192.168.37.10/
HTTP/1.1 200 OK
Server: Sunginx/1.99.0    <--自定义信息
Date: Sat, 06 Aug 2022 07:12:06 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 05 Aug 2022 10:11:41 GMT
Connection: keep-alive
ETag: "62ececdd-264"
Accept-Ranges: bytes
[root@nginx10 ~]# cd /apps/nginx/
[root@nginx10 nginx]# vim conf/nginx.conf
···
http {
    include       /apps/nginx/conf.d/*.conf;    <--配置文件存放路径、以后配置文件从此目录中调用、不用在修改主配置文件
    include       mime.types;
    default_type  application/octet-stream;
...

[root@nginx10 nginx]# mkdir /apps/nginx/conf.d/
#创建配置文件
[root@nginx10 nginx]# vim /apps/nginx/conf.d/test.conf
server_tokens off;   <--隐藏版本

#重新加载nginx
[root@nginx10 nginx]# nginx -s reload
[root@nginx10 ~]# curl -I http://192.168.37.10/
HTTP/1.1 200 OK
Server: Sunginx   <--
Date: Sat, 06 Aug 2022 07:22:39 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 05 Aug 2022 10:11:41 GMT
Connection: keep-alive
ETag: "62ececdd-264"
Accept-Ranges: bytes

ngx_http_core_module (一)

  1. ngx_http_core_module
  2. 与套接字相关的配置:

server { ... }

配置一个虚拟主机
server {
             listen          address[:PORT]|PORT;
             server_name     SERVER_NAME;
             root            /PATH/TO/DOCUMENT_ROOT;
             }
  1. listen PORT|address[:port]|unix:/PATH/TO/SOCKET_FILE
listen address[:port] [default_server] [ssl] [http2 | spdy] [backlog=number][rcvbuf=size] [sndbuf=size];      
   default_server         设定为默认虚拟主机,无法匹配虚拟主机时使用
   ssl                            限制仅能够通过ssl连接提供服务
   backlog=number    超过并发连接数后,新请求进入后援队列的长度
   rcvbuf=size             接收缓冲区大小
   sndbuf=size            发送缓冲区大小   
  注:     
  (1) 基于port;
        listen PORT;               指令监听在不同的端口
  (2) 基于ip的虚拟主机   
        listen IP:PORT;           IP地址不同
  (3) 基于hostname
        server_name fqdn;    指令指向不同的主机名

虚拟主机

nginx

[root@nginx html]# pwd
/usr/share/nginx/html
[root@nginx html]# mkdir /data/site1
[root@nginx html]# mkdir /data/site2
[root@nginx html]# echo /data/site1/index.html > /data/site1/index.html
[root@nginx html]# echo /data/site2/index.html > /data/site2/index.html

图片.png

[root@nginx html]# nginx

centos6测试机

#更改hosts文件、做dns解析\做测试
[root@centos6 ~]$ vim /etc/hosts
192.168.37.7 www.a.net www.a.tach    <--

[root@centos6 ~]$ curl www.a.net
/data/site1/index.html
[root@centos6 ~]$ curl www.a.tech
/data/site2/index.html

修改默认页面

nginx

[root@nginx html]# vim /etc/nginx/nginx.conf
...
    server {
        listen       80;   <--
        listen       [::]:80 default_server;

图片.png

[root@nginx html]# nginx

centos6测试机

此时默认网也路径发生变化

[root@centos6 ~]$ curl 192.168.37.7
/data/site2/index.html

如不修改、恢复到默认即可、默认信息如下

nginx

[root@nginx html]# vim /etc/nginx/nginx.conf
···
    server {
        listen       80 default_server;    <--
        listen       [::]:80 default_server;
···
[root@nginx html]# vim /etc/nginx/conf.d/test.conf 
charset utf-8;
server_tokens off;
server {
    server_name    www.a.net;
    root    /data/site1/;
}

server {
    server_name    www.a.tech;
    root    /data/site2/;
}

ngx_http_core_module (二)

  1. server_name name ...;
  • 虚拟主机的主机名称后可跟多个由空白字符分隔的字符串
  • 支持*通配任意长度的任意字符
server_name    *.a.com    www.a.*
  • 支持~起始的字符做正则表达式模式匹配,性能原因慎用
server_name ~^www\d+\.a\.com$
说明:\d 表示 [0-9]
  • 匹配优先级机制从高到低
(1) 首先是字符串精确匹配 如:www.a.com
(2) 左侧*通配符 如:*.a.com
(3) 右侧*通配符 如:www.a.*
(4) 正则表达式 如:~^.*\.a\.com$
(5) default_server(默认)

nginx

[root@nginx html]# vim /etc/nginx/conf.d/test.conf 
charset utf-8;
server_tokens off;
server {
    server_name    www.a.net;   <--精确匹配
    root    /data/site1/;
}

server {
    server_name    *.a.tech;   <--左侧*通配符
    root    /data/site2/;
}

[root@nginx html]# nginx

centos6测试机

#默认页面
[root@centos6 ~]$ curl 192.168.37.7
welcome to 7
#精确匹配
[root@centos6 ~]$ curl www.a.net
/data/site1/index.html
#左侧*通配符
[root@centos6 ~]$ curl www.a.tech
/data/site2/index.html

ngx_http_core_module (三)

  1. 定义路径相关的配置
  2. root
设置web资源的路径映射;用于指明请求的URL所对应的文档的目录路径,可用于http, server, location, if in location
server {
  ...
  root /data/www/vhost1;
}
示例
   http://www.a.com/images/logo.jpg
            --> /data/www/vhosts/images/logo.jpg

ngx_http_core_module (四)

  1. location [ = | ~ | * | ^ ] uri { ... }

location @name { ... } 在一个server中location配置段可存在多个,用于实现从uri到文件系统的路 径映射;ngnix会根据用户请求的URI来检查定义的所有location,并找出一个最 佳匹配,而后应用其配置

示例:

server {...
      server_name www.a.com;
      location /images/ {
      root /data/imgs/;
      }
}
http://www.a.com/images/logo.jpg 
           --> /data/imgs/images/logo.jpg

nginx

[root@nginx ~]# vim /etc/nginx/conf.d/test.conf 

charset utf-8;
server_tokens off;
server {
    server_name    www.a.net;
    root    /data/site1/;
    location /test {        <--当访问网站下'/test'时
        root /opt/testdir;     <--从'/opt/testdir'中找'/test'
    }
}

server {
    server_name    *.a.tech;
    root    /data/site2/;
}

[root@nginx ~]# mkdir /opt/testdir/test -pv /opt/testdir/test
#启动服务
[root@nginx ~]# nginx

centos6

测试

[root@centos6 ~]$ curl  www.a.net/test/
/opt/testdir/test/index.html

ngx_http_core_module (五)

= 对URI做精确匹配;
       location = / {
           ...
       }
       http://www.a.com/ 匹配
       http://www.a.com/index.html 不匹配
^~               对URI的最左边部分做匹配检查,不区分字符大小写
~                 对URI做正则表达式模式匹配,区分字符大小写 
~*                对URI做正则表达式模式匹配,不区分字符大小写
不带符号     匹配起始于此uri的所有的uri
\                   转义符,可将 . * ?等转义为普通符号
匹配优先级从高到低:
                    =, ^~, ~/~* , 不带符号      

nginx

匹配优先级

#再添加一个'location'
[root@nginx html]# vim /etc/nginx/conf.d/test.conf 
charset utf-8;
server_tokens off;
server {
    server_name    www.a.net;                  <--
    root    /data/site1/;
    location /test/index.html {                  <--不带符号
        root /opt/testdir;
    }
    location = /test/index.html {               <--'=' 精确匹配 访问'www.a.com'时,此处优先级最高
        root /opt/testdir2;                         <--
    }                                                       <--
}

server {
    server_name    *.a.tech;
    root    /data/site2/;
}


#创建目录
[root@nginx ~]# mkdir /opt/testdir2/test -pv
#编辑测试页面
[root@nginx ~]# echo /opt/testdir2/test/index.html > /opt/testdir2/test/index.html
[root@nginx ~]# tree /opt/
/opt/
├── rh
├── testdir
│   └── test
│       └── index.html
└── testdir2
    └── test
        └── index.html

5 directories, 2 files

[root@nginx ~]# nginx

centos6

[root@centos6 ~]$ curl www.a.net/test/index.html
/opt/testdir2/test/index.html

动静分离

nginx

[root@nginx html]# vim /etc/nginx/conf.d/test.conf 

charset utf-8;
server_tokens off;
server {
    server_name    www.a.net;
    root    /data/site1/;
    location ~* \.(jpg|gif|html|txt|js|css)$ {    <--静态文件后缀
        root /opt/static;     <--静态文件存放目录
    }
    location ~ \.(php|jsp|asp) {    <--动态文件后缀
        root /opt/dynamic;     <--动态文件存放目录
    }
}

server {
    server_name    *.a.tech;
    root    /data/site2/;
}

#静态文件夹
[root@nginx html]# mkdir /opt/static
#动态文件夹
[root@nginx html]# mkdir /opt/dynamic
#静态信息
[root@nginx html]# echo a.html > /opt/static/a.html
#动态信息
[root@nginx html]# echo a.jsp > /opt/dynamic/a.jsp
#启动服务
[root@nginx html]# nginx

centos6

测试

#静态页面
[root@centos6 ~]$ curl www.a.net/a.html
a.html
#动态页面
[root@centos6 ~]$ curl www.a.net/a.jsp
a.jsp

location生产案例

生产案例:静态资源配置

location ^~ /static/ {
   root /data/nginx/static;
}
# 或者
location ~* \.(gif|jpg|jpeg|pngbmp|tiff|tif|css|js|ico)$ {
   root /data/nginx/static;
}