搭建web服务器要求如下:
1.web服务器的主机ip:172.25.254.100
ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:05:f0:06 brd ff:ff:ff:ff:ff:ff
altname enp3s0
altname ens160
inet 172.25.254.100/24 brd 172.25.254.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::569d:c66e:bbba:809d/64 scope link noprefixroute
valid_lft forever preferred_lft forever
查看当前电脑的IP地址
2.web服务器的默认访问目录为/var/www/html
默认发布内容为default‘s page
打开配置文件
vim /etc/nginx/nginx.conf
server {
listen 80;
listen [::]:80;
server_name _;
# root /usr/share/nginx/html;
root /var/www/html;
index index.html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
随后可以使用命令 nginx -t 检查一下配置文件的错误
[root@lee ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
然后建立目录文件
[root@lee ~]# mkdir -p /var/www/html
将默认发布内容写进去
[root@lee ~]# echo "default's page" > /var/www/html/index.html
然后可以测试一下
[root@lee ~]# cat /var/www/html/index.html
default's page
3.站点news.timinglee.org默认发布目录为/var/www/virtual/timinglee.org/news
默认发布内容为 news.timinglee.org
打开配置文件
vim /etc/nginx/nginx.conf
将include /etc/nginx/conf.d/*.conf;复制至最后一行
server {
listen 80;
listen [::]:80;
server_name _;
# root /usr/share/nginx/html;
root /var/www/html;
index index.html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location /timinglee/ {
auth_basic on;
auth_basic_user_file "/etc/nginx/.htpasswd";
}
}
include /etc/nginx/conf.d/*.conf;
mkdir -p /etc/nginx/certs
openssl req -newkey rsa:2048 -nodes -sha256 -keyout /etc/nginx/certs/timinglee.org.key -x509 -days 365 -out /etc/nginx/certs/timinglee.org.crt
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:shangxi
Locality Name (eg, city) [Default City]:xi'an
Organization Name (eg, company) [Default Company Ltd]:timinglee
Organizational Unit Name (eg, section) []:webserver
Common Name (eg, your name or your server's hostname) []:www.timinglee.org
Email Address []:admin@timinglee.org
vim /etc/nginx/conf.d/vhosts.conf
server {
listen 80;
server_name news.timinglee.org;
root /usr/share/nginx/virtual/timinglee.org/news;
index index.html;
}
[root@lee ~]# mkdir -p /var/www/virtual/timinglee.org/news
[root@lee ~]# echo news.timinglee.org > /var/www/virtual/timinglee.org/news/index.html
[root@lee ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
4.站点login.timinglee.org在被访问时必须强制走加密协议,其默认发布目录为/var/www/virtual/timinglee.org/login,默认发布内容为login.timinglee.org
vim /etc/nginx/conf.d/vhosts.conf
添加以下代码
server{
listen 80;
server_name login.timinglee.org;
rewrite ^/(.*)$ https://login.timinglee.org/$1 permanent;
}
[root@lee ~]# mkdir -p /var/www/virtual/timinglee.org/login
[root@lee ~]# echo login.timinglee.org > /var/www/virtual/timinglee.org/login/index.html