Nginx的初步认识及配置
什么是Nginx
是一个高性能的反向代理服务器
正向代理代理的是客户端
反向代理代理的是服务端
Apache、Tomcat、Nginx
静态web服务器
jsp/servlet服务器 tomcat
Nginx安装
- 下载tar包
http://nginx.org/download/nginx-1.14.2.tar.gz
- 解压
tar -zxvf nginx-1.14.2.tar.gz
- ./configure [–prefix]
yum install pcre-devel yum install zlib-devel ./configure --prefix=/home/nginx
- 安装
make && make install
启动和停止
1. sbin/nginx
2. ./nginx -s stop
nginx.conf
event
events {
#仅对指定的客户端输出debug级别的日志: 语法:debug_connection[IP|CIDR]
#这个设置项实际上属于事件类配置,因此必须放在events{……}中才会生效。它的值可以是IP地址或者是CIRD地址。
#debug_connection 10.224.66.14; #或是debug_connection 10.224.57.0/24
#这样,仅仅以上IP地址的请求才会输出debug级别的日志,其他请求仍然沿用error_log中配置的日志级别。
#注意:在使用debug_connection前,需确保在执行configure时已经加入了--with-debug参数,否则不会生效。
worker_connections 1024;
}
http
虚拟主机配置
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
基于IP的虚拟主机
基于端口号的虚拟主机
server {
listen 8000;
#listen localhost:8080;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
基于域名的虚拟机
server {
listen 80;
server_name www.abc.com;
location / {
root html;
index index.html;
}
}
server {
listen 80;
server_name bbs.abc.com;
location / {
root html;
index bbs.html;
}
}
server {
listen 80;
server_name ask.abc.com;
location / {
root html;
index ask.html;
}
}
location
配置语法
location [= | ~* | ^~ ] /uri/ {…}
配置规则
location = /uri 精准匹配
location ^~ /uri 前缀匹配
location ~ /uri
location / 通用匹配
规则的优先级
1 location = /
2 location = /index
3 location ^~ /article/
4 location ^~ /article/files/
5 location ~ \.(gif|png|js|css)$
6 location /
http://192.168.11.154/
http://192.168.11.154/index ->2
http://192.168.11.154/article/files/1.txt ->4
http://192.168.11.154/mic.png ->5
- 精准匹配是优先级最高
- 普通匹配(最长的匹配)
- 正则匹配
实际使用建议
location =/ {
}
location / {
}
location ~* \.(gif|....)${
}
Nginx模块
内置模块:反向代理、email、nginx core。。。
模块分类
- 核心模块 ngx_http_core_module
- 标准模块 http模块
- 第三方模块
如何添加第三方模块
- 原来所安装的配置,你必在重新安装新模块的时候,加上
- 不能直接make install
安装方法
./configure --prefix=/安装目录 --add-module = /第三方模块的目录
./configure --prefix=/home/cj/nginx --with-http_stub_status_module --with-
http_random_index_module
cp objs/nginx $nginx_home/sbin/nginx