nginx安装/使用
NGINX 安装/使用
下载&安装
选择稳定版即可
http://nginx.org/en/download.html
解压
tar -zxvf nginx-1.16.1.tar.gz
./configure --without-http_rewrite_module --without-http_gzip_module
PCRE 作用是让 Nginx 支持 Rewrite 功能
configure
对即将安装的软件进行配置,检查当前的环境是否满足要求,成功执行后生成makefile
文件
make && make install
默认安装在
/usr/local/nginx/
常用命令
用法: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives] 选项: -?,-h : 帮助 -v : 显示版本信息,退出 -V : 显示版本信息和配置信息,退出 -t : 检查配置文件语法,退出 -q : suppress non-error messages during configuration testing -s signal : 发送指令,stop停止、quit退出、reopen重启、reload重新加载nginx.conf -p prefix : set prefix path (default: /usr/local/nginx/) -c filename : set configuration file (default: conf/nginx.conf) -g directives : set global directives out of configuration file
nginx.cnf 详解
配置文件最外层就是一个 main{} 省略了而已
worker_processes 8; #处理请求的进程数,初始值可设置为cpu总核数 (cat /proc/cpuinfo| grep "processor"| wc -l) pid logs/nginx.pid; #把进程号记录到文件,用于管理nginx进程 #IO事件模型与worker进程连接数设置 events{ #use epoll; #linux2.6以上内核高性能网络IO模型 worker_connections 1024; #单个worker进程最大连接数 } #http模块设置 http{ include mime.types; #引入mime映射文件 default_type application/octet-stream; #默认文件类型 #日志输出格式 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; #main日志输出到指定文件 sendfile on; #开启高效文件传输模式 keepalive_timeout 65; #连接超时时间,单位(秒) server{ listen 80; #监听的端口 server_name localhost; #域名 location / { # ‘/’表示所有请求 #所有请求代理到指定ip指定端口 proxy_pass http://192.168.1.1:8892/; } #将服务器错误页重定向到静态页/50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { # ‘= /50x.html’ 精准匹配 root html; #这里的html表示相对路径的目录 } } }
目录访问/文件下载
修改配置文件
nginx.conf
server{ listen 80; #监听的端口 server_name localhost; #域名
default_type 'text/html'; #解决中文乱码
charset utf-8; #解决中文乱码 autoindex on; #开启nginx目录浏览功能(默认关闭) autoindex_exact_size off; #关闭显示出文件的确切大小,单位是bytes(默认开启) autoindex_localtime on; #显示文件修改时间为服务器本地时间(默认关闭) location / { # '/' 表示所有请求 root /home; #站点根目录 } #将服务器错误页重定向到静态页/50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
root
根目录autoindex
[on/off] 是否开启目录浏览(默认关闭)autoindex_exact_size
[on/off] #关闭文件以bytes单位显示(默认开启)autoindex_localtime
[on/off] #显示文件修改时间为服务器本地时间(默认关闭)
反向代理
server{ listen 80; #监听的端口 server_name localhost; #域名 location / { # ‘/’表示所有请求 #所有请求代理到指定ip指定端口 proxy_pass http://127.0.0.1:8080; } #将服务器错误页重定向到静态页/50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { # ‘= /50x.html’ 精准匹配 root html; } }
proxy_pass
代理通往指定服务
负载均衡
- 轮询 ——1:1 轮流处理请求(默认)
每个请求按时间顺序逐一分配到不同的应用服务器,如果应用服务器down掉,自动剔除,剩下的继续轮询。
- 权重 ——you can you up
通过配置权重,指定轮询几率,权重和访问比率成正比,用于应用服务器性能不均的情况。
- ip_哈希算法
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个应用服务器,可以解决session共享的问题。
upstream tomcatserver { server 127.0.0.1:8080 down; server 127.0.0.1:8081; server 127.0.0.1:8082 weight=2; server 127.0.0.1:8083 backup; } server{ listen 80; #监听的端口 server_name localhost; #域名 location / { # ‘/’表示所有请求 #所有请求分发到upstream配置的服务 proxy_pass http://tomcatserver; } #将服务器错误页重定向到静态页/50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { # ‘= /50x.html’ 精准匹配 root html; } }
down
表示单前的server暂时不参与负载weight
默认为1,weight越大,负载的权重就越大。backup
其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。