转载自 http://www.dtcms.cn/news/show-219.html
反向代理:
正向代理:代理客户端(如:VPN)
反向代理:代理服务器
Nginx负载均衡策略:
轮询、加权轮询
解决session不共享问题:
iphash:对客户端请求的ip进行hash操作,然后根据hash结果将同一个客户的请求分发给同一台服务器进行处理,可以解决session不共享的问题。
加Redis:
动静分离
下载:
官网下载链接: http://nginx.org/en/download.html.
windowins下安装并启动:
只需要解压到无中文路径即可,有中文会报错,
然后找到nginx.exe的地方,双击nginx.exe即可启动,但是会一闪而过,所以不建议这样启动,建议在此处打开cmd输入nginx.exe
启动(注:start nginx
也可以,建议使用start nginx
)(注意路径)。
默认监听80:
conf目录下的nginx.conf文件中默认监听拦截80端口,此端口根据实际情况去配置:
验证监听:
在浏览器随便输入一个80端口的请求,看到下面界面,说明监听成功。
linux下安装(略复杂)并启动:
1.解压:
tar -zxvf nginx-1.20.0.tar.gz //解压
2.解压以后执行命令: ./configure
注:由于linux安装不同,此处可能会出现缺失依赖库的错误,缺失什么安装什么后再次执行 ./configure
即可,笔者安装时遇到三次错误:
a.
./configure: error: C compiler cc is not found
解决命令:yum -y install gcc gcc-c++ autoconf automake make
b.
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
解决命令:yum -y install pcre-devel
c.
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
解决命令:yum install -y zlib-devel
3.然后分别执行下面两个命令:
make //编译
make install //安装
4.启动
用 whereis nginx
查看nginx安装路径,然后进入路径位置,执行启动命令 ./nginx
验证:(与Windows相似)
注:如果访问不到可能是linux防火墙没有关闭,如何linux防火墙请参考下面文章:
链接: 虚拟机能请求到虚拟的web项目,宿主机却访问不了——linux关闭防火墙.
Nginx常用命令(linux下):
1. cd /usr/local/nginx/sbin #进入命令目录
2. ./nginx #启动
3. ./nginx -s stop #强制退出
4. ./nginx -s quit #安全退出
5. ./nginx -s reload #重新加载配置文件
6. ps aux|grep nginx #查看nginx进程
Nginx常用命令(windows下):
验证配置是否正确: nginx -t
查看Nginx的详细的版本号:nginx -V
查看Nginx的简洁版本号:nginx -v
启动Nginx:start nginx
快速停止或关闭Nginx:nginx -s stop
正常停止或关闭Nginx:nginx -s quit
配置文件修改重装载命令:nginx -s reload
详细配置: https://www.runoob.com/w3cnote/nginx-setup-intro.html.
**
nginx配置文件:
**