一、作用
1、反向代理
a、分向代理:代理服务端,如:用vpn访问网站
b、反向代理:代理服务端,如:nginx
2、apache、tomcat、nginx:都是http-server,都接收http请求
二、安装与运行
1、安装
a、tar -zxvf nginx-1.1.4.0.tar.gz
b、yum install pcre-devel
c、yum install zlib-devel
d、./configure --prefix=/data/program/nginx【安装路径(conf、html、logs、sbin)】
e、make && make install
2、启动停止
a、启动:sbin/nginx
b、停止:nginx -s stop
3、访问:ip:80
三、配置说明
cd conf
1、vim nginx.conf
虚拟主机配置
server{
listen 80;------------------------------------>打开对80端口号的监听
server_name localhost;------------------------>域名配置
location / {---------------------------------->匹配规则,根据用户的uri去匹配
root html;------------------------------->静态资源的访问路径
index index.html;------------------------->首页文件名称
}
}
2、基于ip的虚拟主机
3、基于port的虚拟主机
server{
listen 8080;------------------------------------>打开对8080端口号的监听
server_name localhost;
location / {---------------------------------->匹配规则
root html;------------------------------->静态资源的访问路径
index index.html;------------------------->首页文件名称
}
}
4、基于域名的虚拟主机
server{
listen 8080;------------------------------------>打开对8080端口号的监听
server_name www.sunxj1222.com;
location / {---------------------------------->匹配规则
root html;------------------------------->静态资源的访问路径
index index.html;------------------------->首页文件名称
}
}
5、location匹配规则:
a、精准匹配:location=/index
b、前缀匹配:location ^~ /info
c、正则匹配:location ~ \.(gif|png|css)$
d、通通匹配:location / {}
6、改动配置不需要重启,重新加载就好,不用重启nginx:./nginx -s -reload
7、验证配置语法是否有问题:./nginx -t
8、查看nginx的版本号:./nginx -Vg
四、模块
反向代理、email、nginx core
1、模块分类
a、核心模块:ngx_http_core_module
b、标准模块:http模块
c、第三方模块
2、ngx_http_core_module
server {
listen port
server_name
location
error_page:500 501 /50x.html------------------------>定义错误码的页面
location = /50x.html{
root html;
}
...
}
3、ngx_http_access_module-------------->访问控制模块,deny或allow
location / {
deny all;
allow all;
}
4、http_stub_status_module------------->查看nginx状态的信息,比如client端请求了多少次,server端响应了多少次请求
location /status{
stub_status;
}
5、http_random_index_module------------->随机选择一个页面响应
location /random{
random_index on;
}
6、添加第三方模块
a、原理安装的配置,必须在安装新模块的时候加上
b、不能直接make install
(1)、安装
a、方式一:./configure --perfix=/安装目录 --add-module=/第三方模块的目录
b、方式二:./configure --prefix=/data/program/nginx --with-http_stub_status_module --with-http_random_index_module
c、make
d、cp objs/nginx ../nginx/sbin/nginx