Linux 安装nginx、配置文件及负载均衡

来源:互联网 发布:淘宝运营助理工资多少 编辑:程序博客网 时间:2024/05/23 21:11

1.下载nginx、openssl、zlib、pcre

http://nginx.org/en/download.html
http://www.openssl.org/
http://www.zlib.NET/
http://www.pcre.org/

2.安装编译器

yum install gcc-c++

3.上传 安装

openssl :

tar zxvf openssl-fips-2.0.12.tar.gzcd openssl-fips-2.0.12./config && make && make install

zlib:

tar zxvf zlib-1.2.11.tar.gzcd zlib-1.2.11./configure && make && make install

pcre:

tar zxvf pcre-8.00.tar.gzcd pcre-8.00 ./configure && make && make install

nginx:

tar zxvf nginx-1.8.0.tar.gzcd nginx-1.8.0 ./configure && make && make install

4.启动

查找nginx安装地址

whereis nginx

启动:

 /usr/local/nginx/sbin/nginx

报错:

error while loading shared libraries: libpcre.so.1: cannot open shared
object file: No such file or directory

解决:
先找到libpcre.so.1位置

whereis libpcre.so.1

做软链接

ln -s /usr/local/lib/libpcre.so.1 /lib64

可以顺利启动,访问成功
这里写图片描述

启动:/usr/local/nginx/sbin/nginx
停止/重新加载:/usr/local/nginx/sbin/nginx -s stop(quit、reload)
验证配置文件是否合法:/usr/local/nginx/sbin/nginx -t
命令帮助:/usr/local/nginx/sbin/nginx -h

5.配置文件

proxy_pass
在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走。

server {        listen       80;        server_name  test.abc.com;        location  /proxy/ {                proxy_pass http://127.0.0.1:81/;        }}

访问http:// test.abc.com/proxy/test.html会被代理到http:// 127.0.0.1:81/test.html

server {        listen       80;        server_name  test.abc.com;        location  /proxy/ {                proxy_pass http://127.0.0.1:81;        }}

访问http:// test.abc.com/proxy/test.html会被代理到http:// 127.0.0.1:81/proxy/test.html

server {        listen       80;        server_name  test.abc.com;        location  /proxy/ {                proxy_pass http://127.0.0.1:81/test/;        }}

访问http:// test.abc.com/proxy/test.html会被代理到http:// 127.0.0.1:81/test/test.html

server {        listen       80;        server_name  test.abc.com;        location  /proxy/ {                proxy_pass http://127.0.0.1:81/test;        }}

访问http:// test.abc.com/proxy/test.html会被代理到http:// 127.0.0.1:81/testtest.html 这个url(???http:// 127.0.0.1:81/test/proxy/test.html???)

proxy_redirect

server {        listen       80;        server_name  test.abc.com;        location / {             proxy_pass http://127.0.0.1:81;        } }

这段配置一般情况下都正常,但偶尔会出错, 错误在什么地方呢? 抓包发现服务器给客户端的跳转指令里加了端口号,如 Location: http://test.abc.com:9080/abc.html 。因为nginx服务器侦听的是80端口,所以这样的URL给了客户端,必然会出错.针对这种情况, 加一条proxy_redirect指令: proxy_redirect http://test.abc.com:9080/ / ,把所有“http://test.abc.com:9080/”的内容替换成“/”再发给客户端,就解决了。

   server {        listen       80;        server_name  test.abc.com;        proxy_redirect http://test.abc.com:9080/ /;        location / {             proxy_pass http://127.0.0.1:81;        }    } 

在代替的字段中可以不写服务器名

upstream
配置ngnix负载均衡。server1负载均衡服务器,server2和server3作为web server

upstream web_servers {    server server2:80 weight=5;    server server3:80 weight=10;}server {    listen  server1:80;    location / {        proxy_pass http://web_servers;    }}

负载均衡有如下算法:
1. 轮询
2. 权重轮询:见上例
3. IP Hash : 需要在upstream的配置中指定“ip_hash;”,每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
4. fair:需要在upstream的配置中指定“fair;”按后端服务器的响应时间来分配请求,响应时间短的优先分配。
5. URL hash:需要在upstream中配置指定:“hash $request_uri; hash_method crc32;”按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
另外,在nignx中可以为每个 backend server 指定最大的重试次数和重试时间间隔。所使用的关键字是 max_fails 和 fail_timeout。而backup指的是仅当其他serverdown或者忙时才会请求这台server。

原创粉丝点击