Nginx是一个web服务器,功能与tomcat相似。除此之外还有Apache,Jetty,IIS。
Tomcat,Jetty都是面向java语言的,先天就是重量级的web服务器。
IIS只能在Windows上运行的web服务器。
Apache是目前世界上第一大web服务器,其发展较早,当时的设计并没有考虑到现在互联网时代下的高并发场景。
安装环境:
Nginx 依赖GCC 编译器,PCRE 库,Zlib库 ,OpenSSL库
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel
下载nginx
wget http://nginx.org/download/nginx-1.14.0.tar.gz
解压:
tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0
配置:
./configure
构建:
make
安装:
make install
默认目录下启动nginx:
cd /usr/local/nginx/
./sbin/nginx
在浏览其中访问Nigx:
添加新模块:
cd /packages/
cd nginx-1.14.0
查看现有模块:
cd objs/
ls -la
配置新模块:
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-debug
make
查看新加模块:
cd objs/
ls -la
停止nginx:
/usr/local/nginx/sbin/nginx -s stop
用加完新模块的nginx来替换老nginx:
cp nginx /usr/local/nginx/sbin/nginx
再次启动nginx:
cd /usr/local/nginx/
./sbin/nginx
我的理解:
www.mp.csdn.net/console/editor/html/106530556
如上,对于一个网址,我认为第一个/之前的为host控制部分,第一个/以及之后的都由location控制。所以对于nginx的配置中的server_name的配置,如下:
server_name www.allen.com *.allen.com www.allen.*;
可以匹配进入的的serverName便可以进入如下server对应匹配的server,
例子:如下,配置了4个server,nigix会根据客户端传入server的网址/前面的server名路由到下面不同的server中去,
1. 添加如下代码到nginx.conf中去。
server {
listen 80;
server_name www.red.com www.red.*;
location / {
root html;
index red.html;
}
}
server {
listen 80;
server_name www.green.com www.green.*;
location / {
root html;
index green.html;
}
}
server {
listen 80;
server_name www.blue.com www.blue.*;
location / {
root html;
index blue.html;
}
}
server {
listen 80;
server_name www.pink.com www.pink.*;
location / {
root html;
index pink.html;
}
}
2. 编辑html文件。
[root@allen1 html]# pwd
/usr/local/nginx/html
[root@allen1 html]# ls -la
-rw-r--r--. 1 root root 48 Jun 13 16:21 blue.html
-rw-r--r--. 1 root root 50 Jun 13 16:21 green.html
-rw-r--r--. 1 root root 612 Jun 1 17:58 index.html
-rw-r--r--. 1 root root 48 Jun 13 16:22 pink.html
-rw-r--r--. 1 root root 46 Jun 13 16:13 red.html
[root@allen1 html]#
[root@allen1 html]# cat red.html
<h1 ><font color="red"> I'am red </font></h1>
[root@allen1 html]#
2. 重新加载conf文件 ./sbin/nginx -s reload
3. 添加host域名映射。
192.168.0.101 www.red.com
192.168.0.101 www.red.cn
192.168.0.101 www.green.com
192.168.0.101 www.green.cn
192.168.0.101 www.pink.com
192.168.0.101 www.pink.cn
192.168.0.101 www.blue.com
192.168.0.101 www.blue.cn
4. 测试不同的域名访问。
location的配置:
语法规则: location [=|~|~*|^~] /uri/ { … }
=
开头表示精确匹配
^~ /static/ /aa匹配到(注意是空格)。以xxxx开头
~
开头表示区分大小写的正则匹配 以xx结尾
~*
开头表示不区分大小写的正则匹配 以xx结尾
!~
分别为区分大小写不匹配
!~*
不区分大小写不匹配 的正则
/
通用匹配,任何请求都会匹配到。
1. = 开头表示精确匹配。
location = / {
root html;
index hello1.html;
}
location = /baidu.html {
proxy_pass http://www.baidu.com;
}
(1) 直接访问/。会进入hello1.html
(2) 输入http://www.allen.com/baidu.html, 会跳转到百度官网。
2. ^~ /static/ /aa匹配到(注意是空格)。如下例子,以static 开头,配置文件如下:
location ^~ /static {
root /usr/local/nginx/;
index hello.html;
}
创建需要访问的静态文件
访问静态文件
3. ~
开头表示区分大小写的正则匹配。如下匹配有图片文件。
location ~ \.(jpg|gif|png|js)$ {
root files;
index hello.html;
}
访问allen.jpg,会自动从files 文件夹中获取文件。
4. ~*
开头表示不区分大小写的正则匹配
location ~* \.(png|jpg)$ {
root pngfile;
index hello.html;
}