简介
以前听过nginx,但是一直没接触过,现在有空,特地去了解并且基本的去使用nginx,网上很多配置介绍,自己写一篇加深理解以防忘记。
我主要是去了解怎么用nginx去实行反向代理。简单说下代理和反向代理吧。
代理:
主要是代理客户端。
例如:一个公司只有一个外网ip,但是可能有几十到上百台电脑,其中每台电脑都需要有上网的功能。那么此时就可以弄个代理服务器,公司中的每一个电脑的上网请求全部发送到这个代理服务器,代理服务器在利用外网ip去根据不同电脑执行不同的操作。相当就是个中介。这就是代理。
反向代理:
主要是代理服务器。
最通俗易懂的例子:假如你想查你手机还剩多少余额。你只需拨打对应的的运营商电话就可以了(10086/10010/10000),此时,你并不知道接你电话的这个客服代表是男是女,是帅是丑。因为他们有个类似代理服务器的系统,会自动把你的通话转接到空闲的客服那儿去。同理,一个服务器也是这样的,就像淘宝,在双11的时候,订单太多,一个服务器是处理不过来的。他会把请求分别分发到相对比较空闲的服务器上去(实际上不知道复杂多少倍.......)。
图解如下,是在网上copy的一个图...
配置
官网下载 http://nginx.org/en/download.html我这儿是下载的1.10.3的windows版本。下载完解压,找到conf文件夹下 nginx.conf。在里面进行配置。
1、在默认请求中添加代理服务器配置proxy_pass 和proxy_redirect
#默认请求
location / {
#定义服务器的默认网站根目录位置
root html;
#定义首页索引文件的名称
index index.html index.htm;
#代理服务器的配置。与上诉的负载均衡的名要相同
proxy_pass http://demo.com;
# 对发送的URL进行修改(很多时候代理的路径出问题可以尝试修改此处)
proxy_redirect default;
}
2、我这儿是同一个电脑的两个不同tomcat服务器,更改端口,在tomcat的conf目录下找到server.xml更改 Connector。具体如下。也可以是不同电脑的不同路径,以及各种的负载均衡配置,在下一篇介绍。
<!--tomcat默认端口配置-->
<Connector port="8088" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
改完之后添加一个负载均衡配置,具体如下。地址只能到端口。我第一次配置想在端口后面加上具体的项目名,结果导致nginx始终无法启动。#负载均衡配置。有多种配置方式,名称与下面的配置要相同
upstream demo.com{
#服务 地址 权重,数字越大,被访问到的比例越大
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8088 weight=1;
}
整个配置文件以及详解如下
#user nobody; #运行用户
#启动进程,一般设置为和cpu的数量相等
worker_processes 1;
#全局错误日志和pid文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#工作模式以及连接上限数
events {
#单个后台worker process进程的最大并发链接数
worker_connections 1024;
#并发总数(max_clients)为 进程数worker_process乘以worker_connections
#如果作为服务器的话这个并发总数(max_clients)要除以2。作为反向代理的话要除以4
#为什么除以2:该公式基于http 1.1协议,一次请求大多数浏览器发送两次连接,并不是request和response响应占用两个线程
#为什么除以4:因nginx作为方向代理,客户端和nginx建立连接,nginx和后端服务器也要建立连接
#worker_connerctions设置的值和物理内存大小有关。因为max_clients的数量必须得小于系统可打开的最大文件数
#系统可打开的文件数和内存是成正比关系。实际最后的并发总量和配置的有一定的偏差。因为系统中还有其他的进程在运行。
}
http {
#设定mime类型,类型由mime.type文件定义
include mime.types;
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;
#sendfile可以指定mginx是否调用sendfile函数来输出文件,对应普通应用设置为on,
#如果用来进行下载等磁盘io负载较重的应用,可设置为off,可以平衡磁盘和网络I/O的处理速度
#详细解释如下
#设置为on表示启动高效传输文件的模式。sendfile可以让Nginx在传输文件时直接在磁盘和tcp socket之间传输数据。
#如果这个参数不开启,会先在用户空间(Nginx进程空间)申请一个buffer,用read函数把数据从磁盘读到cache,
#再从cache读取到用户空间的buffer,再用write函数把数据从用户空间的buffer写入到内核的buffer,
#最后到tcp socket。开启这个参数后可以让数据不用经过用户buffer。
sendfile on;
#tcp_nopush on;
#连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
#负载均衡配置。有多种配置方式,名称与下面的配置要相同
upstream demo.com{
#服务 地址 权重,数字越大,被访问到的比例越大
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8088 weight=1;
}
#是否开启gzip压缩
#gzip on;
#虚拟主机配置
server {
#监听端口
listen 80;
#定义使用localhost访问
server_name localhost;
#charset koi8-r;
#设定本虚拟主机的访问日志
#access_log logs/host.access.log main;
#默认请求
location / {
#定义服务器的默认网站根目录位置
root html;
#定义首页索引文件的名称
index index.html index.htm;
#代理服务器的配置。与上诉的负载均衡的名要相同
proxy_pass http://demo.com;
# 对发送的URL进行修改(很多时候代理的路径出问题可以尝试修改此处)
proxy_redirect default;
}
#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
#
#PHP 脚本请求全部转发到FastCGI处理,使用FastCGI默认配置
#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
#
#禁止访问.ht xxx文件
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
配置完后直接点击nginx.exe启动,或者在命令行中键入到对应的文件位置执行 start -nginx,启动后在进程中能看到以下两个进程。
随后打开浏览器访问localhost:80就能看到效果了。具体效果如下,我是在同一台电脑上开了两个tomcat,端口分别是8080和8088。有两个不同的初始页面,每次访问localhost:80的时候会随机访问其中一个tomcat。