nginx暑假实训

[toc]

1.Nginx 安装

一、安装编译工具及库文件

1
2
3
4
5
#安装编译环境:gcc gcc-c++
#安装openssl-devel(使nginx支持ssl)
#安装lib
yum -y install make zlib zlib-devel gcc gcc-c++ libtool openssl openssl-devel

二、首先要安装 PCRE

PCRE 作用是让 Nginx 支持 Rewrite 功能。

1
yum -y install pcre pcre-devel

三、安装 Nginx

1、下载 Nginx,下载地址:https://nginx.org/en/download.html

1
# 找到nginx-1.22.0这个稳定包,下载下来,然后用rz上传到服务器

2、解压安装包

1
tar -xzvf nginx-1.22.0.tar.gz

3、进入安装包目录

1
cd nginx-1.22.0

4、编译

1
./configure --prefix=/usr/local/nginx

5、安装

1
make && make install

6、查看nginx版本

1
/usr/local/nginx/sbin/nginx -v

四、启动Nginx

进入安装好的目录/usr/local/nginx/sbin

1
2
3
4
./nginx           启动
./nginx -s stop 快速关闭
./nginx -s quit 优雅关闭,在退出前完成已经接受的连接请求
./nginx -s reload 重新加载配置

五、关于防火墙和SELINUX

关闭防火墙

1
systemctl stop firewalld.service

禁止防火墙开机自启

1
systemctl stop firewalld.service
1
2
3
4
5
6
7
#关闭SELINUX
sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
#修改完成之后重启服务器,然后getenforce看看
#当然如果不想重启服务器也可以先修改好配置,然后用 setenforce 0来临时关闭SELINUX,下次重启自然会识别配置文件了。
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive

六、添加systemctl管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cat << EOF > /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStartPre=/bin/mkdir /tmp/nginx
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF

七、创建环境变量

1
2
3
4
5
vim /etc/profile.d/nginx.sh

NGINX_HOME=/usr/local/nginx
PATH=$NGINX_HOME/sbin:$PATH
export NGINX_HOME PATH

source /etc/profile.d/nginx.sh

启动nginx

1
2
mkdir -p /tmp/nginx
nginx

八、修改ip为静态地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="ff458f3b-f132-400d-ae06-86277ce43612"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=192.168.110.131 # 静态ip地址
NETMASK=255.255.255.0 # 子网掩码
GATEWAY=192.168.110.1 # 网关地址
BROADCAST=192.168.110.255 # 广播地址
BOOTPROTO="static" # 修改静态

2.Nginx的conf学习

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
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
more /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;
}


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;

# 虚拟服务器 这里开始
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;
#}
}
# 虚拟服务器 这里结束

# 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;
# }
#}

}

虚拟主机

虚拟主机应用场景:一个WEB服务器同时发布多个WEB站点

用ip基于端口访问
1
2
3
4
5
6
7
8
9
10
# 在跟目录下放置www目录,然后内置两个目录,分别是www目录和void目录
# 写入不同的html页面
[root@localhost /]# cd www
[root@localhost www]# ls
void www
[root@localhost www]# cat void/index.html
this is void web site...
[root@localhost www]# cat www/index.html
this is www web site...
[root@localhost www]#

修改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
[root@localhost www]# vim /usr/local/nginx/conf/nginx.conf

server {
listen 80; #网页访问端口80
server_name localhost;
location / {
root /www/www; #网址指定目录
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 81; #网页访问端口81
server_name localhost;
location / {
root /www/void; #网址指定目录
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
用域名基于端口访问
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@localhost www]# vim /usr/local/nginx/conf/nginx.conf

server {
listen 80; #网页访问端口80
server_name www.julintongxue.top;#域名
location / {
root /www/www; #网址指定目录
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 81; #网页访问端口81
server_name void.julintongxue.top;#域名
location / {
root /www/void; #网址指定目录
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}