nginx web服务器的搭建

安装相关环境
1
2
3
4
[root@iZuf6iq8e7ya9v3ix71k0pZ ~]# yum install gcc-c++
[root@iZuf6iq8e7ya9v3ix71k0pZ ~]# yum install -y pcre pcre-devel
[root@iZuf6iq8e7ya9v3ix71k0pZ ~]# yum install -y zlib zlib-devel
[root@iZuf6iq8e7ya9v3ix71k0pZ ~]# yum install -y openssl openssl-devel
下载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
[root@iZuf6iq8e7ya9v3ix71k0pZ ~]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
[root@iZuf6iq8e7ya9v3ix71k0pZ ~]# tar zxvf nginx-1.12.2.tar.gz
[root@iZuf6iq8e7ya9v3ix71k0pZ ~]# ll nginx-1.12.2
总用量 724
drwxr-xr-x 6 www www 4096 3月 19 21:47 auto
-rw-r--r-- 1 www www 278202 10月 17 21:16 CHANGES
-rw-r--r-- 1 www www 423948 10月 17 21:16 CHANGES.ru
drwxr-xr-x 2 www www 4096 3月 19 21:47 conf
-rwxr-xr-x 1 www www 2481 10月 17 21:16 configure
drwxr-xr-x 4 www www 4096 3月 19 21:47 contrib
drwxr-xr-x 2 www www 4096 3月 19 21:47 html
-rw-r--r-- 1 www www 1397 10月 17 21:16 LICENSE
drwxr-xr-x 2 www www 4096 3月 19 21:47 man
-rw-r--r-- 1 www www 49 10月 17 21:16 README
drwxr-xr-x 9 www www 4096 3月 19 21:47 src
[root@iZuf6iq8e7ya9v3ix71k0pZ ~]# cd nginx-1.12.2


# 注意:启动nginx之前,上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录
# 第三步:进入nginx目录,使用configure命令创建一makeFile文件
[root@iZuf6iq8e7ya9v3ix71k0pZ ~]# cd nginx-1.12.2 ./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

[root@iZuf6iq8e7ya9v3ix71k0pZ nginx-1.12.2]# ll
总用量 732
drwxr-xr-x 6 www www 4096 3月 19 21:47 auto
-rw-r--r-- 1 www www 278202 10月 17 21:16 CHANGES
-rw-r--r-- 1 www www 423948 10月 17 21:16 CHANGES.ru
drwxr-xr-x 2 www www 4096 3月 19 21:47 conf
-rwxr-xr-x 1 www www 2481 10月 17 21:16 configure
drwxr-xr-x 4 www www 4096 3月 19 21:47 contrib
drwxr-xr-x 2 www www 4096 3月 19 21:47 html
-rw-r--r-- 1 www www 1397 10月 17 21:16 LICENSE
-rw-r--r-- 1 root root 355 3月 19 22:22 Makefile
drwxr-xr-x 2 www www 4096 3月 19 21:47 man
drwxr-xr-x 3 root root 4096 3月 19 22:22 objs
-rw-r--r-- 1 www www 49 10月 17 21:16 README
drwxr-xr-x 9 www www 4096 3月 19 21:47 src

# 有个makefile,make编译
[root@iZuf6iq8e7ya9v3ix71k0pZ nginx-1.12.2]# make

# 安装
[root@iZuf6iq8e7ya9v3ix71k0pZ nginx-1.12.2]# make install
[root@iZuf6iq8e7ya9v3ix71k0pZ nginx-1.12.2]# cd /usr/local/nginx/
[root@iZuf6iq8e7ya9v3ix71k0pZ nginx]# ll
总用量 12
drwxr-xr-x 2 root root 4096 3月 19 22:25 conf
drwxr-xr-x 2 root root 4096 3月 19 22:25 html
drwxr-xr-x 2 root root 4096 3月 19 22:25 sbin

# 启动nginx
[root@iZuf6iq8e7ya9v3ix71k0pZ nginx]# cd sbin/
[root@iZuf6iq8e7ya9v3ix71k0pZ sbin]# ll
总用量 3608
-rwxr-xr-x 1 root root 3692808 3月 19 22:25 nginx
[root@iZuf6iq8e7ya9v3ix71k0pZ sbin]# ./nginx
[root@iZuf6iq8e7ya9v3ix71k0pZ sbin]# ps -aux|grep nginx
root 4775 0.0 0.0 20488 612 ? Ss 22:27 0:00 nginx: master process ./nginx
nobody 4776 0.0 0.0 20928 1332 ? S 22:27 0:00 nginx: worker process
root 4785 0.0 0.0 112664 976 pts/1 R+ 22:27 0:00 grep --color=auto nginx

# 关闭nginx
[root@iZuf6iq8e7ya9v3ix71k0pZ sbin]# ./nginx -s stop
[root@iZuf6iq8e7ya9v3ix71k0pZ sbin]# ps -aux|grep nginx
root 4801 0.0 0.0 112664 976 pts/1 S+ 22:30 0:00 grep --color=auto nginx
[root@iZuf6iq8e7ya9v3ix71k0pZ sbin]#

# 不关闭情况下,重新加载配置
[root@iZuf6iq8e7ya9v3ix71k0pZ sbin]# ./nginx -s reload
启动成功测试
通过端口区分虚拟主机
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
[root@iZuf6iq8e7ya9v3ix71k0pZ nginx]# cp -r html/ html81
[root@iZuf6iq8e7ya9v3ix71k0pZ nginx]# vim conf/nginx.conf
server {
listen 81;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html81;
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;
}

}
[root@iZuf6iq8e7ya9v3ix71k0pZ nginx]# sbin/nginx -s reload
通过域名区分虚拟主机
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
[root@iZuf6iq8e7ya9v3ix71k0pZ nginx]# cp -r html/ html-xyz
[root@iZuf6iq8e7ya9v3ix71k0pZ nginx]# vim conf/nginx.conf
server {
listen 80;
server_name smniuhe.xyz;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html-xyz;
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;
}

}
[root@iZuf6iq8e7ya9v3ix71k0pZ nginx]# sbin/nginx -s reload
反向代理
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
[root@iZuf6iq8e7ya9v3ix71k0pZ nginx]# vim conf/nginx.conf
upstream tomcat8081 {
server smniuhe.com:8081;
}


server {
listen 80;
server_name smniuhe.com;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
proxy_pass http://tomcat8081;
index index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@iZuf6iq8e7ya9v3ix71k0pZ nginx]# sbin/nginx -s reload
负载均衡

weight:设置对应服务器的权重,默认为1

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
[root@iZuf6iq8e7ya9v3ix71k0pZ nginx]# vim conf/nginx.conf
upstream tomcat8081 {
server smniuhe.com:8081 weight=1;
server smniuhe.com:8083 weight=1;
}


server {
listen 80;
server_name smniuhe.com;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
proxy_pass http://tomcat8081;
index index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@iZuf6iq8e7ya9v3ix71k0pZ nginx]# sbin/nginx -s reload