Nginx简述
Nginx是一个开源且高性能的,可靠的HTTP中间件,代理服务。
Nginx用途
正向代理:客户需要在客户端做正向代理配置,然后访问目的网站。例如:VPN。
反向代理:用户首选访问代理服务器,代理服务器将请求转发给真实服务器,此过程用户是无感的。
负载均衡:单个服务器解决不了,我们增加服务器的数量,然后把请求分发到各个服务器上。
动静分离:为了加快网站的解析速度,可以把动态界面和静态界面分不同的服务器解析,加快解析速度,降低原服务器的压力。
Nginx常用命令
Linux命令
./nginx -v
查看nginx版本号
ps -ef | grep nginx
查看nginx进程
./nginx
启动nginx
./nginx -s stop
关闭nginx
./nginx -s reload
重新加载nginx
Windows命令
nginx -v
查看nginx版本号
start nginx
启动nginx
nginx -s stop
快速停止或关闭nginx
nginx -s quit
正常停止或关闭nginx
nginx -s reload
配置文件nginx.conf修改重装载命令
nginx.config配置文件讲解
nginx配置文件主要由3部分组成:
第一部分全局块
从配置文件的头开始到events之间的部分,主要设置一些影响nginx服务器运行的配置指令,主要包括配置运行nginx服务器的用户,允许生成work process数,进程PID存放路径,日志存放路径和类型以及配置文件的引入。
worker_processes 数字越大 表示可以支持的并发越大
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
第二部分events块
events模块主要负责nginx服务器与用户之间的网络连接。
例如worker_connections 1024;
表示nginx支持的最大链接数是1024。
events {
worker_connections 1024;
}
第三部分HTTP块
nginx配置中最频繁的部分。HTTP块包括HTTP全局快和Service快。
HTTP全局块为:
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;
Service快为:
server {
listen 80; #表示nginx目前监听的端口号是80端口号
server_name localhost; #表示主机名称
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
Nginx配置反向代理
案例1
当我们访问localhost:80时,界面对跳转taobao.com地址。地址栏显示的是127.0.0.1但是访问到淘宝服务器。
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
proxy_pass https://taobao.com;
index index.html index.htm;
}
}
案例2
nginx服务器监听127.0.0.1地址和8000端口,当监听的地址中如果带有one就会被分配到http://127.0.0.1:8081服务上如图1所示。如果监听地址中有two就会被分配置http://127.0.0.1:8082服务上如图2所示。
server {
listen 8000;
listen 127.0.0.1;
location ~/one/ {
proxy_pass http://127.0.0.1:8081;
}
location ~/two/ {
proxy_pass http://127.0.0.1:8082;
}
}


Nginx配置负载均衡
1.轮询
轮询是一种默认的策略,每个请求按照不同的顺序分配到不同的服务器上,如果服务器down掉,则自动移除。
upstream server_test{
server 127.0.0.1:8081;
server 127.0.0.1:8082;
}
location / {
proxy_pass http://server_test;
root html;
index index.html index.htm;
}
首先配置upstream 服务名称,然后在location里面proxy_pass http://后面加上前面配置的服务名。
2.weight
weight代表权重,权重越大代表分给客户端的请求越多
upstream server_test{
server 127.0.0.1:8081 weight=1;
server 127.0.0.1:8082 weight=2;
}
3.ip_hash
每个请求根据访问的ip的hash分配,每个请求可以固定的访问session共享问题问题。
upstream server_test{
ip_hash
server 127.0.0.1:8081;
server 127.0.0.1:8082;
}
3.fair
根据响应时间分配,响应事假越短优先分配。
upstream server_test{
server 127.0.0.1:8081;
server 127.0.0.1:8082;
fair;
}
Nginx动静分离
如图3所示nginx把动态的请求发送到Tomcat服务器,吧静态的请求转发到静态资源服务器。(图3是盗的)

配置说明:通过不同的location指定不同的后缀实现不同的路径转发。通过expires参数可以设置浏览器的过期时间,较少对服务器的请求。
upstream dynamic_request{
server 192.168.1.1;
}
upstream static_request {
server 192.169.1.2;
}
server {
listen 80;
server_name 127.0.0.1;
location / {
proxy_pass http://dynamic_request;
include proxy_params;
}
location ~* \.(png|gif)$ {
proxy_pass http://static_request;
include proxy_params;
}
}
网友评论