1.创建下载路径
mkdir /usr/local/nginx
2.进入nginx路径下
cd /usr/local/nginx
3.通过wget命令下载nginx压缩包
wget -c https://nginx.org/download/nginx-1.10.1.tar.gz
4.安装gcc-c++
yum install gcc-c++
5.安装pcre pcre-devel
yum install -y pcre pcre-devel
6.安装zlib
yum install -y zlib zlib-devel
7.安装open ssl
yum install -y openssl openssl-devel
8.进入/usr/local/nginx路径下,在解压 nginx-1.10.1.tar.gz
cd /usr/local/nginx
tar -zxvf nginx-1.10.1.tar.gz
9.进入解压文件
cd nginx-1.10.1
10默认配置
./configure
11.编译安装首先进入目录(/usr/local/nginx-1.10.1)
cd /usr/local/nginx-1.10.1
make
make install
12.进入/usr/local/nginx/sbin目录
cd /usr/local/nginx/sbin
13.启动nginx(在/usr/local/nginx/sbin目录 )
./nginx
14.关闭nginx
./nginx -s quit
或者
./nginx -s stop
15.重启nginx
./nginx -s reload
16.查看进程
ps aux|grep nginx
负载均衡测试
cd /usr/local/nginx
cp -r html /html2/
cd html2/
vim index.html
cd /usr/local/nginx/conf
修改配置文件
#user nobody;
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 {
worker_connections 1024;
}
http {
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 on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream test-server {
#least_conn;
server localhost:8080;
server localhost:8081;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://test-server;
proxy_redirect default;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 8080;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 8081;
server_name localhost;
location / {
root html2;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# 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
curl 127.0.0.1 测试