web服务的部署及高级优化方案

本文详细描述了如何配置Web服务器,包括设置主机IP、指定默认访问目录、配置默认页面以及实现SSL加密和基本身份验证。教程中涉及了Nginx的配置步骤和创建虚拟主机的过程。
摘要由CSDN通过智能技术生成

搭建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
 

  • 8
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值