nginx配置和使用
1 正向代理服务器 vpn/ chrome设置
反向代理服务器 nginx
apache服务器 静态页面服务器
nginx服务器 反向代理服务器
tomcat服务器 jsp、servlet服务器
2 nginx 安装 配置
下载
解压
./configure 配置
$ ./configure --prefix=/usr/local/nginx 安装后的所有资源源文件都会被放在/usr/local/nginx目录中,不会分散到其他目录。 使用--prefix选项的另一个好处是方便卸载软件或移植软件
不指定prefix,则可执行文件默认放在/usr /local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc。其它的资源文件放在/usr /local/share。你要卸载这个程序,要么在原来的make目录下用一次make uninstall(前提是make文件指定过uninstall),要么去上述目录里面把相关的文件一个个手工删掉。
指定prefix,直接删掉一个文件夹就够了。
make &&make install 编译 ,安装
启动 停止
./sbin/nginx
./sbin/nginx -s stop
修改nginx.conf 后 ./sbin/nginx -s reload
配置 nginx.conf
Main
event
线程模型
http
日志
虚拟主机配置
ip 基于虚拟ip
基于端口
server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } } #原本的是这个样子的端口80跟目录为html,首页为index.htm server { listen 81; server_name localhost; location / { root cart; index cart.html; } }
基于域名
server { listen 80; server_name www.aaa.com; location / { root html; index index.html index.htm; } } #原本的是这个样子的端口80跟目录为html,首页为index.html server { listen 80; server_name bbs.aaa.com; location / { root html; index bbs.html; } }
#windows下hosts文件的作用是本地域名解析,从域名到ip 如果查不到则到远程dns服务器查询获取ip 主域名 二级域名
location 精准匹配 location = /uri location = /index.html{
root html;
index index.html} 前缀匹配 location ^~/uri uri匹配 location ~/uri location ~\.(gif|png|css) $ {
}动静分离 通用匹配 location location / {...}
Nginx模块
核心模块 比如 ngx_http_core_module 提供服务 server error_page
比如 ngx_http_access_module deny all 等
安装模块:需要重新安装 不能直接make & make intall
./configure --prefix=... --with-模块名 --with-模块名 #官方集成 如果是第三方模块 需要用add-module
make & make install
标准模块 http
第三方模块
反向代理配置
proxy_set_header可以多配置获取原始客户端ip 或者 调用链上多个ip
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
负载均衡配置
nginx(http)/lvs/HaProxy(tcp层)
权重
server ip:8080 weight=1;
server ip2:8080 weight=2;
ipHash
ip_hash;
server ip:8080 max_fails=2 fail_timeout=60s;# 优化配置
server ip2:8080;
动静分离配置
../conf/mime.types 静态资源类型
可以配置多个location
location 精准匹配 location = /uri location = /index.html{ root html; index index.html} uri匹配 location ~/uri location ~\.(gif|png|css) $ { }
缓存 expires
压缩gzip on;
防盗链 :
跨域访问:
location里配置
add_header 'Access-Control-Origin' '*';
add_header ...
add_header ...
进程模型:
master进程 1
worker进程 多个 cpu核心数
多路复用
event配置
events{
use epoll;
worker_connection1024;
} #worker * connection
kivealived 主备 安装kivealived 软件模块
vip
vrrp
openRestry安装使用
LVS 4层负载均衡模块