(Linux)搭建静态网站——基于http/https协议的静态网站

打印 上一主题 下一主题

主题 1012|帖子 1012|积分 3036

简朴了解nginx配置文件


1.下载并开启nginx服务


  • 下载
    [root@localhost ~]# dnf install nginx -y
  • 开启
    [root@localhost ~]# systemctl restart nginx
1.(1)搭建静态网站——基于http协议的静态网站

实验1:搭建一个web服务器,访问该服务器时显示“hello world”欢迎界面


  • 进入指定目录文件下,/usr/share/nginx/html 是 Nginx Web 服务器的默认根目录,用于存放静态文件。当你访问配置了 Nginx 的域名或 IP 地点时,Nginx 会从这个目录中查找并返回相应的文件。
    [root@localhost ~]# cd /usr/share/nginx/html
  • 写入指定内容"hello world"
    1. [root@localhost html]# echo "hello world" > index.html
    2. #或者
    3. [root@localhost html]# vim index.html
    4. hello world
    复制代码
  • 使用 curl 工具来发送 HTTP 请求,并获取相应(验证是否配置成功)
    1. #向本地服务器发送一个 HTTP 请求,并返回服务器的响应
    2. [root@localhost html]# curl localhost
    3. hello world
    4. #或者
    5. #向该 IP 地址发送一个 HTTP 请求,并返回服务器的响应。
    6. [root@localhost html]# curl 192.168.190.131
    7. hello world
    复制代码
实验2:创建两个基于ip地点访问的网站,要求如下



  • 该网站ip地点的主机位为100,设置首页目录为/www/ip/100,网页内容为:this is 100
  • 该网站ip地点主机位为200,设置首页目录为/www/ip/200,网页内容为:this is 200

  • 创建文件
    1. [root@localhost ~]# mkdir -pv /www/ip/{1,2}00
    2. mkdir: created directory '/www'
    3. mkdir: created directory '/www/ip/100'
    4. mkdir: created directory '/www/ip/200'
    5. #显示目录 /www/ 的状态信息
    6. [root@localhost ~]# stat /www/
    7.   File: /www/
    8.   Size: 32                Blocks: 0          IO Block: 4096   directory
    9. Device: fd00h/64768d        Inode: 102252363   Links: 4
    10. Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
    11. Context: unconfined_u:object_r:default_t:s0
    12. Access: 2024-11-06 16:33:32.107790537 +0800
    13. Modify: 2024-11-06 16:33:32.109790471 +0800
    14. Change: 2024-11-06 16:33:32.109790471 +0800
    15. Birth: 2024-11-06 16:33:32.107790537 +0800
    复制代码
  • 添加IP地点
    1. [root@localhost ~]# nmcli connection modify ens160 +ipv4.addresses 192.168.168.100/24 +ipv4.gateway 192.168.168.2 ipv4.dns 114.114.114.114 ipv4.method manual autoconnect yes
    2. [root@localhost ~]# nmcli connection modify ens160 +ipv4.addresses 192.168.190.200/24
    3. #激活ens160的网络连接。
    4. [root@localhost ~]# nmcli connection up ens160
    复制代码
    或者:
    [root@localhost ~]# nmtui–使用图形化工具更便捷



  • 配置子配置文件
    setenforce 0 命令将 selinux 的模式设置为 Permissive 模式。在 Permissive 模式下,selinux 会记录违反计谋的行为,但不会阻止这些行为。换句话说,系统会继续实行操纵,但会记录下全部违反 selinux 计谋的行为。
    1. [root@localhost ~]# cd /etc/nginx/conf.d
    2. [root@localhost conf.d]# vim test_ip.conf
    3. server {
    4.         listen 192.168.190.100:80;
    5.         server_name _;
    6.         root /www/ip/100;
    7. }
    8. server {
    9.         listen 192.168.190.200:80;
    10.         server_name _;
    11.         root /www/ip/200;
    12. }
    13. #设置selinux,必须设置,否则无法看到网页页面内容
    14. [root@localhost conf.d]# setenforce 0
    15. [root@localhost conf.d]# curl 192.168.190.100
    16. this is 100
    17. [root@localhost conf.d]# curl 192.168.190.200
    18. this is 200
    复制代码
实验3:创建两个基于不同端口访问的网站,要求如下:



  • 创建一个使用web服务器默认端口的网站,设置网站首页目录为/www/port/80,网页内容为:the port is 80。
  • 创建一个使用10000端口的网站,设置网站首页目录为/www/port/10000,网页内容为:the port is 10000。
  1. #创建文件
  2. [root@localhost ~]# mkdir /www/port/{80.10000}
  3. #对应分别写入index.html内容
  4. [root@localhost ~]# echo this port is 80 > /www/port/80/index.html
  5. [root@localhost ~]# echo this port is 10000 > /www/port/10000/index.html
  6. #切换目录(便于操作)
  7. [root@localhost ~]# cd /etc/nginx/conf.d/
  8. #在conf文件中写入服务连接
  9. [root@localhost conf.d]# vim test_port.conf
  10. server {
  11.         listen 192.168.190.10:80;
  12.         server_name _;
  13.         root /www/port/80;
  14. }
  15. server {
  16.         listen 192.168.190.10:10000;
  17.         root /www/port/10000;
  18.         location / {}
  19. }   
  20. #重启服务生效
  21. [root@localhost ~]# systemctl restart nginx
  22. #用curl验证是否生效
  23. [root@localhost ~]# curl 192.168.190.10:80
  24. the port is 80
  25. [root@localhost ~]# curl 192.168.190.10:10000
  26. the port is 10000
复制代码
实验4:创建两个基于域名访问的网站,要求如下:



  • 新建一个网站,域名为www.ceshi.com,设置网站首页目录为/www/name,网页内容为this is test。
  • 新建一个网站,域名为rhce.first.day,同时可通过ce.first.day访问,设置网站首页目录为/www/ce,网页内容为:today is first day of class
  1. #创建目录文件
  2. [root@localhosts ~]# mkdir /www/{name,ce}
  3. #写内容到index.html
  4. [root@localhosts ~]# echo today is first day of class >> /www/ce/index.html
  5. [root@localhosts ~]# echo this is test >> /www/name/index.html
  6. #创建并编辑网页访问文件
  7. [root@localhosts conf.d]# vim test_servername.conf
  8. #添加如下内容
  9. server {
  10.         listen 192.168.190.10:80;
  11.         server_name www.ceshi.com;
  12.         root /www/name;
  13. }
  14. server {
  15.         listen 192.168.190.10:80;
  16.         server_name rhce.first.day ce.first.day;
  17.         root /www/ce;
  18.         location / {}
  19. }      
  20. [root@localhosts conf.d]# vim /etc/hosts
  21. #添加如下内容
  22. 192.168.190.10 localhosts www.ceshi.com rhce.first.day ce.first.day
  23. #查看修改的内容是否有问题
  24. [root@localhosts conf.d]# nginx -t
  25. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  26. nginx: configuration file /etc/nginx/nginx.conf test is successful
  27. #重启服务
  28. [root@localhosts conf.d]# systemctl restart nginx
  29. #访问
  30. [root@localhosts conf.d]# curl www.ceshi.com
  31. this is test
  32. [root@localhosts conf.d]# curl rhce.first.day
  33. today is first day of class
  34. [root@localhosts conf.d]# curl ce.first.day
  35. today is first day of class
复制代码
实验5:基于虚拟目录和用户控制的web网站

  1. #网页认证自动生成储存用户密码和用户名的文件---需要下载httpd-tools包来提供相应的服务
  2. [root@localhosts ~]# dnf install httpd-tools -y
  3. #创建用户
  4. [root@localhosts nginx]# htpasswd -cb /etc/nginx/conf.d/auth-password user1 123
  5. #新建文件目录--作为实际访问目录,并写入实际访问的内容index.html
  6. [root@localhosts ~]# mkdir /www/real
  7. [root@localhosts ~]# echo real > /www/real/index.html
  8. #编辑网页访问
  9. [root@localhosts conf.d]# vim  test_virtual.conf
  10. server{
  11.         listen 192.168.190.131:80;
  12.         root /usr/share/nginx/index;
  13.         location /real {
  14.                 alias /www/real;
  15.                 auth_basic on;
  16.                 auth_basic_user_file /etc/nginx/conf.d/auth_password;
  17.         }
  18. }
  19. [root@localhosts conf.d]# nginx -t
  20. [root@localhosts conf.d]# systemctl restart nginx
  21. #访问
  22. [root@localhosts conf.d]#  curl user1:123@192.168.190.131/real/
  23. real
  24. [root@localhosts conf.d]#  curl 192.168.190.131/real/ -u user1
  25. Enter host password for user 'user1':
  26. real
复制代码
1.(2)搭建静态网站——基于https协议的静态网站

(1).添加IP

  1. # 添加ip
  2. [root@localhosts ~]# nmcli connection modify ens160 +ipv4.addresses 192.168.190.20/24
  3. # 激活更新网卡
  4. [root@localhosts ~]# nmcil c up ens160
复制代码
(2).创建访问路径

  1. [root@localhosts ~]# mkdir -pv /www/https
  2. # 写入index.html内容
  3. [root@localhosts ~]# echo https > /www/https/index.html
复制代码
(3).生成私钥和证书

/etc/pki/tls/certs/ 目录通常存储与 TLS(传输层安全性)相关的证书文件,这些文件在 Linux 系统中用于确保安全通讯
  1. [root@localhosts ~]# cd /etc/pki/tls/certs/
  2. # 得到一个包含新生成的 RSA 私钥的文件 https.key
  3. [root@localhosts certs]# openssl genrsa -out https.key
  4. #生成一个自签名的 X.509 证书,并将其保存到名为 https.crt 的文件中
  5. [root@localhosts certs]# openssl req -utf8 -new -key https.key -x509 -days 100 - https.crt
  6. # 查看是否成功
  7. [root@localhosts certs]# ls
  8. ca-bundle.crt  ca-bundle.trust.crt  https.crt  https.key
复制代码
(4).配置网页访问配置文件

  1. [root@localhosts certs]# vim /etc/nginx/conf.d/test_https.conf
  2. [root@localhosts certs]# cat /etc/nginx/conf.d/test_https.conf
  3. server {
  4.         listen 192.168.190.20:443 ssl;
  5.         root /www/https;
  6.         ssl_certificate /etc/pki/tls/certs/https.crt;
  7.         ssl_certificate_key /etc/pki/tls/certs/https.key;
  8.         location / {}
  9. }
  10. # 检验.conf文件是否能够生效
  11. [root@localhosts certs]# nginx -t
  12. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  13. nginx: configuration file /etc/nginx/nginx.conf test is successful
  14. #重启生效
  15. [root@localhosts certs]# systemctl restart nginx
复制代码
(5).访问测试

  1. [root@localhosts certs]# curl --insecure https://192.168.190.20
  2. https
  3. [root@localhosts certs]# curl -k https://192.168.190.20
  4. https
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

卖不甜枣

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表