1:安装工具包 wget、vim和gcc
yum install -y wget
yum install -y vim-enhanced
yum install -y make cmake gcc gcc-c++
2:下载nginx安装包
wget http://nginx.org/download/nginx-1.9.7.tar.gz
- 1
3:安装依赖包
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
- 1
- 2
- 3
4:解压nginx-1.9.7.tar.gz到/usr/local/目录下
tar -zxvf nginx-1.9.7.tar.gz -C /usr/local/
- 1
5:进行configure配置
进入nginx-1.9.7目录然后在执行./configure命令
[root@MiWiFi-R3-srv nginx-1.9.7]# ./configure --prefix=/usr/local/nginx
./configure --with-http_stub_status_module --with-stream (TCP负载均衡用到)
- 1
6:编译安装
[root@MiWiFi-R3-srv nginx-1.9.7]# make && make install
7:启动Nginx,启动完之后检查nginx是否已经正常启动,看到如下信息说明正常启动
[root@MiWiFi-R3-srv nginx-1.9.7]# /usr/local/nginx/sbin/nginx
[root@MiWiFi-R3-srv nginx-1.9.7]# ps -ef | grep nginx
root 24956 1 0 19:41 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 24957 24956 0 19:41 ? 00:00:00 nginx: worker process
root 24959 10533 0 19:41 pts/0 00:00:00 grep --color=auto nginx
[root@MiWiFi-R3-srv nginx-1.9.7]#
如果要关闭nginx,我们可以使用如下命令:
[root@MiWiFi-R3-srv nginx-1.9.7]# /usr/local/nginx/sbin/nginx -s stop
- 1
如果想要重新热启动nginx,则使用如下命令:
[root@MiWiFi-R3-srv nginx-1.9.7]# /usr/local/nginx/sbin/nginx -s reload
8:学习nginx配置
在nginx目录下进入conf目录,该目录下有个nginx.conf文件,这是nginx最重要的配置文件
[root@MiWiFi-R3-srv conf]# vim /usr/local/nginx/conf/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;
}
#TCP负载均衡配置
stream {
upstream tcp_test {
server 192.168.1.114:10011;
server 192.168.1.108:11004;
}
server {
listen 16000; # 需要监听的端口
proxy_timeout 20s;
proxy_pass tcp_test;
}
}
#HTTP负载均衡配置
http {
include mime.types;
default_type application/octet-stream;
upstream server1 {
server 192.168.1.250:8888;
server 192.168.1.250:8889;
}
#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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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
#
#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
#
#location ~ /\.ht {
# deny all;
#}
}
server {
listen 8000;
server_name server1;
location / {
proxy_pass http://server1;
}
}
}