小知识:详解Centos7.2安装Nginx实现负载平衡

下载Nginx

到官网下载源码文件,地址:http://nginx.org/en/download.html,选择最新版本。本人下载的地址为:http://nginx.org/download/nginx-1.10.2.tar.gz,可用wget命令下载,也可以在windows系统上下载好再传到linux上。

卸载httpd

如果系统默认安装了httpd服务,卸载之。不卸载也没关系,这里只是方便默认80端口的处理。

?
1
yum -y remove httpd

解压

?
1
tar -xzvf nginx-xxxxxx.tar.gz

安装编译器和依赖库

?
1
yum install gcc gcc-c++ zlib-devel pcre-devel openssl-devel openssl-libs openssl -y

如果已经安装,就不必了

安装前配置

cd命令转到解压后的目录下。

?
1
./configure –prefix=/usr/local/nginx

这样安装时系统就会把Nginx安装到/usr/local/nginx目录下。

编译

?
1
make

安装

?
1
make install

安装完成,接下来配置环境变量以后就不用使用绝对路径来操作Nginx了:

?
1
vim /etc/profile.d/http.sh

加入以下内容:

?
1
export PATH=/usr/local/nginx/sbin:$PATH

生效配置:

?
1
source !$

启动Nginx

?
1
nginx

nginx -s 后跟stop、reload来关闭和重载nginx,直接运行nginx则启动服务。  如果启动时提示端口被占用,则需要找出被占用的进程,或者更改/usr/local/nginx/conf/nginx.conf文件里的侦听端口。

访问Nginx

在浏览器上输入 http://ip:port 如果出现“Welcome to nginx!”字样,则证明安装成功。如果访问不了,先确认防火墙是否禁止相应端口了。

负载平衡配置示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#user nobody;
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid    logs/nginx.pid;
events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
worker_connections 1024;#最大连接数
}
http {
include    mime.types;#文件扩展名与文件类型映射表,此映射表主要用于部署在本nginx上的静态资源
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    on;
#tcp_nopush   on;
#keepalive_timeout 0;
keepalive_timeout 65;#连接超时时间
gzip on;
#反向代理
#【配置1】此配置是[配置4]和[配置5]的结合
#此配置将请求转发到两个WEB服务器,根据客户端IP分配目标主机,同时按权重分配流量
upstream app1 {
ip_hash;
server 192.168.14.132:8080 weight=5;
server 192.168.14.133:80 weight=3;
}
#【配置2】
#默认负载平衡配置,nginx应用HTTP负载平衡来分发请求。
#upstream app1 {
#  server 192.168.14.132:8080;
#  server 192.168.14.133:80;
#}
#【配置3】
#最小连接负载平衡配置,nginx将尽量不使用繁忙的服务器,而是将新请求分发给不太忙的服务器。
#upstream app1 {
#  least_conn;
#  server 192.168.14.132:8080;
#  server 192.168.14.133:80;
#}
#【配置4】
#会话持久性配置,使用ip-hash,客户端的IP地址用作散列密钥,
#以确定应为客户端请求选择服务器组中的哪个服务器。
#此方法确保来自同一客户端的请求将始终定向到同一服务器,除非此服务器不可用。
#upstream app1 {
#  ip_hash;
#  server 192.168.14.132:8080;
#  server 192.168.14.133:80;
#}
#【配置5】
#加权负载平衡配置,通过使用服务器权重进一步影响nginx负载平衡算法。
#未配置权重的服务器,意味着所有指定的服务器被视为对特定负载平衡方法同等资格。
#upstream app1 {
#  ip_hash;
#  server 192.168.14.132:8080 weight=3;
#  server 192.168.14.133:80 weight=2;
#  server 192.168.14.134:80;
#  server 192.168.14.135:80;
#}
server {#可配置多个server以监听不同IP和不同端口
listen    80;#监听的端口
server_name localhost;#监听的服务器
#charset koi8-r;
#access_log logs/host.access.log main;
#反斜杆代表所有连接,此配置目的是将所有连接交给名为app1的upstream代理,实现负载平衡
location / {
proxy_pass http://app1;
}
#图片文件路径,一般来说,静态文件会部署在本机以加快响应速度
#可配置多个这样的location,满足各种需求
location ~\.(gif|jpg|png)$ {
root /home/root/images;
}
location ~\.(iso|zip|txt|doc|docx)$ {
root /home/root/files;
}
#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;
}
# FastCGI是CGI全称是“公共网关接口”(Common Gateway Interface)
#对于我来说,使用Tomcat代替即可,请忽略此配置。
#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;
#}
# 添加黑名单,禁止某某访问特定文件
# concurs with nginxs one
#
#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;
#  }
#}
}

配置完后,记得执行以下命令生效配置

?
1
nginx -s reload

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.centoscn.com/nginx/2016/1206/8262.html

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:linux中叹号命令(!)的使用小结

2023-3-21 5:15:49

建站知识

小知识:Linux yum 命令安装mysql8.0的教程详解

2023-3-21 5:24:04

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索