网站内容建设与管理,泰安房产网签,北京优化网站建设,所有的竞价托管公司简单了解nginx配置文件 1.下载并开启nginx服务
下载 [rootlocalhost ~]# dnf install nginx -y开启 [rootlocalhost ~]# systemctl restart nginx
1.(1)搭建静态网站——基于http协议的静态网站
实验1#xff1a;搭建一个web服务器#xff0c;访问该服务器时显示“hello w…简单了解nginx配置文件 1.下载并开启nginx服务
下载 [rootlocalhost ~]# dnf install nginx -y开启 [rootlocalhost ~]# systemctl restart nginx
1.(1)搭建静态网站——基于http协议的静态网站
实验1搭建一个web服务器访问该服务器时显示“hello world”欢迎界面 进入指定目录文件下/usr/share/nginx/html 是 Nginx Web 服务器的默认根目录用于存放静态文件。当你访问配置了 Nginx 的域名或 IP 地址时Nginx 会从这个目录中查找并返回相应的文件。 [rootlocalhost ~]# cd /usr/share/nginx/html 写入指定内容hello world [rootlocalhost html]# echo hello world index.html
#或者
[rootlocalhost html]# vim index.html
hello world使用 curl 工具来发送 HTTP 请求并获取响应验证是否配置成功 #向本地服务器发送一个 HTTP 请求并返回服务器的响应
[rootlocalhost html]# curl localhost
hello world
#或者
#向该 IP 地址发送一个 HTTP 请求并返回服务器的响应。
[rootlocalhost html]# curl 192.168.190.131
hello world实验2建立两个基于ip地址访问的网站要求如下
该网站ip地址的主机位为100设置首页目录为/www/ip/100网页内容为this is 100该网站ip地址主机位为200设置首页目录为/www/ip/200网页内容为this is 200 创建文件 [rootlocalhost ~]# mkdir -pv /www/ip/{1,2}00
mkdir: created directory /www
mkdir: created directory /www/ip/100
mkdir: created directory /www/ip/200#显示目录 /www/ 的状态信息
[rootlocalhost ~]# stat /www/File: /www/Size: 32 Blocks: 0 IO Block: 4096 directory
Device: fd00h/64768d Inode: 102252363 Links: 4
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:default_t:s0
Access: 2024-11-06 16:33:32.107790537 0800
Modify: 2024-11-06 16:33:32.109790471 0800
Change: 2024-11-06 16:33:32.109790471 0800Birth: 2024-11-06 16:33:32.107790537 0800添加IP地址 [rootlocalhost ~]# 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[rootlocalhost ~]# nmcli connection modify ens160 ipv4.addresses 192.168.190.200/24 #激活ens160的网络连接。
[rootlocalhost ~]# nmcli connection up ens160或者 [rootlocalhost ~]# nmtui–利用图形化工具更便捷 配置子配置文件 setenforce 0 命令将 selinux 的模式设置为 Permissive 模式。在 Permissive 模式下selinux 会记录违反策略的行为但不会阻止这些行为。换句话说系统会继续执行操作但会记录下所有违反 selinux 策略的行为。 [rootlocalhost ~]# cd /etc/nginx/conf.d
[rootlocalhost conf.d]# vim test_ip.conf
server {listen 192.168.190.100:80;server_name _;root /www/ip/100;
}
server {listen 192.168.190.200:80;server_name _;root /www/ip/200;
}
#设置selinux必须设置否则无法看到网页页面内容
[rootlocalhost conf.d]# setenforce 0
[rootlocalhost conf.d]# curl 192.168.190.100
this is 100
[rootlocalhost conf.d]# curl 192.168.190.200
this is 200实验3建立两个基于不同端口访问的网站要求如下
建立一个使用web服务器默认端口的网站设置网站首页目录为/www/port/80网页内容为the port is 80。建立一个使用10000端口的网站设置网站首页目录为/www/port/10000网页内容为the port is 10000。
#创建文件
[rootlocalhost ~]# mkdir /www/port/{80.10000}
#对应分别写入index.html内容
[rootlocalhost ~]# echo this port is 80 /www/port/80/index.html
[rootlocalhost ~]# echo this port is 10000 /www/port/10000/index.html
#切换目录便于操作
[rootlocalhost ~]# cd /etc/nginx/conf.d/
#在conf文件中写入服务连接
[rootlocalhost conf.d]# vim test_port.conf
server {listen 192.168.190.10:80;server_name _;root /www/port/80;
}server {listen 192.168.190.10:10000;root /www/port/10000;location / {}
}
#重启服务生效
[rootlocalhost ~]# systemctl restart nginx
#用curl验证是否生效
[rootlocalhost ~]# curl 192.168.190.10:80
the port is 80
[rootlocalhost ~]# curl 192.168.190.10:10000
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
#创建目录文件
[rootlocalhosts ~]# mkdir /www/{name,ce}
#写内容到index.html
[rootlocalhosts ~]# echo today is first day of class /www/ce/index.html
[rootlocalhosts ~]# echo this is test /www/name/index.html
#创建并编辑网页访问文件
[rootlocalhosts conf.d]# vim test_servername.conf
#添加如下内容
server {listen 192.168.190.10:80;server_name www.ceshi.com;root /www/name;
}
server {listen 192.168.190.10:80;server_name rhce.first.day ce.first.day;root /www/ce;location / {}
}
[rootlocalhosts conf.d]# vim /etc/hosts
#添加如下内容
192.168.190.10 localhosts www.ceshi.com rhce.first.day ce.first.day
#查看修改的内容是否有问题
[rootlocalhosts conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#重启服务
[rootlocalhosts conf.d]# systemctl restart nginx
#访问
[rootlocalhosts conf.d]# curl www.ceshi.com
this is test
[rootlocalhosts conf.d]# curl rhce.first.day
today is first day of class
[rootlocalhosts conf.d]# curl ce.first.day
today is first day of class实验5基于虚拟目录和用户控制的web网站
#网页认证自动生成储存用户密码和用户名的文件---需要下载httpd-tools包来提供相应的服务
[rootlocalhosts ~]# dnf install httpd-tools -y
#创建用户
[rootlocalhosts nginx]# htpasswd -cb /etc/nginx/conf.d/auth-password user1 123
#新建文件目录--作为实际访问目录并写入实际访问的内容index.html
[rootlocalhosts ~]# mkdir /www/real
[rootlocalhosts ~]# echo real /www/real/index.html
#编辑网页访问
[rootlocalhosts conf.d]# vim test_virtual.conf
server{listen 192.168.190.131:80;root /usr/share/nginx/index;location /real {alias /www/real;auth_basic on;auth_basic_user_file /etc/nginx/conf.d/auth_password;}}
[rootlocalhosts conf.d]# nginx -t
[rootlocalhosts conf.d]# systemctl restart nginx
#访问
[rootlocalhosts conf.d]# curl user1:123192.168.190.131/real/
real
[rootlocalhosts conf.d]# curl 192.168.190.131/real/ -u user1
Enter host password for user user1:
real1.(2)搭建静态网站——基于https协议的静态网站
(1).添加IP
# 添加ip
[rootlocalhosts ~]# nmcli connection modify ens160 ipv4.addresses 192.168.190.20/24
# 激活更新网卡
[rootlocalhosts ~]# nmcil c up ens160(2).创建访问路径
[rootlocalhosts ~]# mkdir -pv /www/https
# 写入index.html内容
[rootlocalhosts ~]# echo https /www/https/index.html(3).生成私钥和证书
/etc/pki/tls/certs/ 目录通常存储与 TLS传输层安全性相关的证书文件这些文件在 Linux 系统中用于确保安全通信
[rootlocalhosts ~]# cd /etc/pki/tls/certs/
# 得到一个包含新生成的 RSA 私钥的文件 https.key
[rootlocalhosts certs]# openssl genrsa -out https.key
#生成一个自签名的 X.509 证书并将其保存到名为 https.crt 的文件中
[rootlocalhosts certs]# openssl req -utf8 -new -key https.key -x509 -days 100 - https.crt
# 查看是否成功
[rootlocalhosts certs]# ls
ca-bundle.crt ca-bundle.trust.crt https.crt https.key(4).配置网页访问配置文件
[rootlocalhosts certs]# vim /etc/nginx/conf.d/test_https.conf
[rootlocalhosts certs]# cat /etc/nginx/conf.d/test_https.conf
server {listen 192.168.190.20:443 ssl;root /www/https;ssl_certificate /etc/pki/tls/certs/https.crt;ssl_certificate_key /etc/pki/tls/certs/https.key;location / {}
}
# 检验.conf文件是否能够生效
[rootlocalhosts certs]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#重启生效
[rootlocalhosts certs]# systemctl restart nginx(5).访问测试
[rootlocalhosts certs]# curl --insecure https://192.168.190.20
https
[rootlocalhosts certs]# curl -k https://192.168.190.20
https