如何基于nginx组建多个子目录网站

华子目录

实验要求

  • 组建多个子目录网站www.openlab.com,该网站有2个子目录www.openlab.com/sxhktwww.openlab.com/zy
  • www.openlab.com/sxhkt使用http读取
  • www.openlab.com/zy使用https读取

实验步骤

  • 准备工作
[root@server ~]# setenforce 0

[root@server ~]# systemctl stop firewalld

[root@server ~]# systemctl disable firewalld

[root@server ~]# yum install nginx -y

[root@server ~]# systemctl start nginx

[root@server ~]# systemctl enable nginx
  • 创建网页目录
[root@server ~]# mkdir -p /www/sxhkt
[root@server ~]# mkdir -p /www/zy

#使用mobaxterm上传网页数据
  • Windows端建立本地hosts域名映射
  • 在这里插入图片描述
  • 建立sxhkthttp网站
[root@server ~]# vim /etc/nginx/nginx.conf
    server {
        listen       80;
        listen       [::]:80;
        server_name  www.openlab.com;
        location /sxhkt {
                alias  /www/sxhkt;
                index index.html index.htm;
        }
        return 301 https://www.openlab.com;    #输入http跳转到https

        # 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 {
        }
    }
  • 建立zyhttps网站
#先制作私钥
#在/etc/nginx目录下制作证书所用的私钥文件zy.key
[root@server ~]# openssl genrsa -aes128 2048 > /etc/nginx/zy.key
Enter PEM pass phrase:                        #输入加密私钥的密码12345
Verifying - Enter PEM pass phrase:            #再输一遍


#再制作证书 (证书需要用CA的私钥进行加密,所以在制作证书之前先制作私钥,证书中含有网站的公钥)
[root@server ~]# openssl req -utf8 -new -key  /etc/nginx/zy.key  -x509  -days  365  -out  /etc/nginx/zy.crt
Enter pass phrase for /etc/nginx/sxhkt.key:       #输入加密私钥的密码12345
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:86     #国家代码
State or Province Name (full name) []:shanxi   #省份
Locality Name (eg, city) [Default City]:xian    #城市
Organization Name (eg, company) [Default Company Ltd]:openlab  #公司
Organizational Unit Name (eg, section) []:rhce    #部门
Common Name (eg, your name or your server's hostname) []:server   #主机名
Email Address []:and@qq.com   #邮箱


#在加载ssl支持的nginx并使用上述私钥时必须去除设置的私钥密码12345
[root@server ~]# cd /etc/nginx
[root@server nginx]# cp zy.key zy.key.org  #先做备份
[root@server nginx]# openssl rsa -in zy.key.org -out zy.key  #去除密码
Enter pass phrase for sxhkt.key.org:         #输入加密私钥的密码12345
writing RSA key


[root@server ~]# vim /etc/nginx/nginx.conf
    server {
        listen       443 ssl http2;
        listen       [::]:443 ssl http2;
        server_name  www.openlab.com;
        location /zy {
                alias  /www/zy;
                index index.html index.htm;
        }

        ssl_certificate "/etc/nginx/zy.crt";
        ssl_certificate_key "/etc/nginx/zy.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers PROFILE=SYSTEM;
        ssl_prefer_server_ciphers on;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
  • 重启服务,测试
[root@server ~]# systemctl restart nginx

#在Windows端浏览器上输入www.openlab.com/sxhkt和www.openlab.com/zy,其中www.openlab.com/zy会跳转到https://www.openlab.com/zy

在这里插入图片描述
在这里插入图片描述

相关推荐

  1. nginx配置服务

    2024-05-04 03:00:01       56 阅读
  2. nginx配置(前端)

    2024-05-04 03:00:01       10 阅读
  3. Nginx虚拟主机部署脚本

    2024-05-04 03:00:01       25 阅读
  4. Nginx配置前端项目

    2024-05-04 03:00:01       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-04 03:00:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-04 03:00:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-04 03:00:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-04 03:00:01       20 阅读

热门阅读

  1. 一些不错的技术网站(持续更新)

    2024-05-04 03:00:01       14 阅读
  2. 力扣-977.有序数组的平方

    2024-05-04 03:00:01       9 阅读
  3. PostgreSQL的扩展pgpool

    2024-05-04 03:00:01       13 阅读
  4. pyflink filter

    2024-05-04 03:00:01       10 阅读
  5. 【AI学习】人工智能 or 人造智能 or 人创智能

    2024-05-04 03:00:01       11 阅读
  6. 你用过最好用的AI工具有哪些?【模板】

    2024-05-04 03:00:01       10 阅读
  7. React 之 如何启动一个新的项目(六)

    2024-05-04 03:00:01       13 阅读