Nginx简单使用

简介

​ Nginx是高性能的轻量级WEB服务器,由于其提供HTTP代理和反向代理、负载均衡、缓存等一系列重要特性,从而广泛应用于当今的WEB服务之中。

下载

1
wget http://nginx.org/download/nginx-1.14.0.tar.gz

安装

安装第三方库

1
2
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
yum -y install pcre-devel

安装成功以后可以查看pcre版本: pcre-config —version

解压安装包

1
tar zxvf nginx-1.14.0.tar.gz

从configure脚本自动生成Makefile

1
2
cd /root/nginx-1.14.0
./configure --prefix=/usr/local/webserver/nginx

这里的 —prefix 选项是指定Nginx的安装路径,这里安装到路径:/usr/local/webserver/nginx

编译安装

1
2
cd /root/nginx-1.14.0
make && make install

查看Nginx版本

1
2
/usr/local/webserver/nginx/sbin/nginx -v
nginx version: nginx/1.14.0

查看安装后的目录结构

总共有四个目录:confhtmllogssbin

启动Nginx

1
/usr/local/webserver/nginx/sbin/nginx

访问主机IP出现以下页面就说明成功了

图1

常用指令

测试nginx测试文件是否正确

1
/usr/local/webserver/nginx/sbin/nginx -t

指定配置文件启动

1
/usr/local/webserver/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

重启

1
2
/usr/local/webserver/nginx/sbin/nginx -s reload            # 重新载入配置文件
/usr/local/webserver/nginx/sbin/nginx -s reopen #重启 Nginx

停止

1
/usr/local/webserver/nginx/sbin/nginx -s stop              # 停止 Nginx

配置文件nginx.conf

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
// 全局配置
user nobody nobody;
worker_processes 3;
error_log logs/error.log;
pid logs/nginx.pid;

// events块配置
events {
use epoll;
worker_connections 1024;
}

// http块配置
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;
keepalive_timeout 65;

// 虚拟主机1:基于域名codesheep.com
server {
listen 8088;
server_name codesheep;

access_log /codesheep/webserver/server1/log/access.log;
error_page 404 /404.html;

location /server1/location1 {
root /codesheep/webserver;
index index.server1-location1.htm;
}

location /server1/location2 {
root /codesheep/webserver;
index index.server1-location2.htm;
}

}

// 虚拟主机2:基于IP地址:192.168.31.177
server {
listen 8089;
server_name 192.168.31.177;

access_log /codesheep/webserver/server2/log/access.log;
error_page 404 /404.html;

location /server2/location1 {
root /codesheep/webserver;
index index.server2-location1.htm;
}

location /srv2/loc2 {
alias /codesheep/webserver/server2/location2/;
index index.server2-location2.htm;
}

location = /404.html {
root /codesheep/webserver/;
index 404.html;
}
}
}

很明显,在上述配置文件中配置了两个虚拟主机:一个 基于域名一个基于IP地址

为了验证该配置的正确性,我们对照此配置,构建一个与其对应的静态站点,其目录结构如下:

1
2
3
4
5
6
7
8
9
10
11
12
/codesheep/webserver
404.html
server1
location1
index.server1-location1.htm
location2
index.server1-location2.htm
server2
location1
index.server2-location1.htm
location2
index.server2-location2.htm

现在可以启动Nginx服务器,并在浏览器中进行测试

测试站点的访问

通过域名访问Server1的Location1和Location2

成功说明配置文件中虚拟主机1配置生效!

域名可以通过本地DNS服务器来解析识别

再访问Server2的Location1和Location2

成功说明配置文件中虚拟主机2配置生效!