排错:yum安装软件提示invalid version flag:if
解决方案:很可能是yum仓库配置有问题,可以仔细检查/etc/yum.repos.d/CentOS-Base.repo 文件中有无错误,我的虚拟机没啥东西所以干脆直接重新安装了一遍yum仓库
Nginx服务安装
第一步:配置nginx仓库
[root@123456 code]# cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
如果提示什么密钥验证失败,则需手动下载密钥
手动下载密钥
wget --no-check-certificate -O /tmp/nginx_signing.key https://nginx.org/keys/nginx_signing.key
导入密钥
rpm --import /tmp/nginx_signing.key
第二部:安装nginx
yum -y install nginx
查看版本号
[root@123456 code]# nginx -v
nginx version: nginx/1.26.1
第三步:配置nginx服务:
配置nginx主文件:
/etc/nginx/nginx.conf
[root@123456 code]# cat /etc/nginx/nginx.conf
user nginx; 启动nginx的虚拟用户,默认存在
worker_processes auto; 启动子进程的数量,根据cpu的核心数
error_log /var/log/nginx/error.log notice; 错误日志
pid /var/run/nginx.pid; 进程pid存放的目录
events {
worker_connections 1024; 进程的最大连接数
}
http {
include /etc/nginx/mime.types; 支持的文件类型
default_type application/octet-stream; 不支持的文件就下载
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main; nginx的访问目录
sendfile on; 文件高效传参
#tcp_nopush on;
keepalive_timeout 65; 超过65秒无请求就断开连接,四次挥手
#gzip on; 是否开启压缩
include /etc/nginx/conf.d/*.conf; 将每个网站的配置单独存为 xxx.conf 文件放在 /etc/nginx/conf.d/ 目录下
}
主机配置文件(业务配置)
/etc/nginx/conf.d/*.conf
默认创建一个default.conf,如果要配置多个可以创建
[root@123456 code]# cat /etc/nginx/conf.d/default.conf
server {
listen 80; 监听端口
server_name ssssssssssssame.com; 如果用户访问此网站,就匹配/下的指定内容
#access_log /var/log/nginx/host.access.log main;
location / {
root /code/; 用户去/code/目录下找东西
index index.html index.htm; 默认返回一个html 或者htm
}
第四步:检查配置并开启nginx
修改完成后使用nginx -t检查语法
[root@123456 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
开启nginx
systemctl start nginx
查看端口服务是否开启:
netstat -tnulp|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8038/nginx: master
nginx配置完成后,我们尝试打开业务网站
配置完成后,我发现还是打不开网站,很可能是以下原因:
1本地hosts没有设置
2防火墙设置
3selinux没有关闭
层层排错后,打开成功
用户访问时,如果提示404或403错误:
404:说明连code目录都没创建
403:说明code目录下没有东西
nginx如何配置多个业务
方式1.使用多IP地址的方式(了解)
方式2.使用多端口的方式
方式3.使用多域名的方式(常用)
方式1.使用多IP地址配置多个业务
流程:假设服务器有2个ip,然后要配置两个conf文件,去一一对应IP地址
1.临时添加ip
[root@123456 code]# ip a add 192.168.74.127/24 dev eno16777736
2.创建两个conf文件
[root@123456 conf.d]# ls
game1.conf game2.conf
3.修改配置文件,绑定ip,格式 ip:端口
[root@123456 conf.d]# vim game1.conf
server {
listen 192.168.74.128:80;
[root@123456 conf.d]# vim game2.conf
server {
listen 192.168.74.127:80;
4.重启nginx服务,查看状态
[root@123456 conf.d]# systemctl restart nginx
[root@123456 conf.d]# netstat -tnulp|grep nginx
tcp 0 0 192.168.74.127:80 0.0.0.0:* LISTEN 11582/nginx: master
tcp 0 0 192.168.74.128:80 0.0.0.0:* LISTEN 11582/nginx: master
5.浏览器按照ip访问
192.168.74.127
192.168.74.128
方法2.基于多端口的方式配置多业务
流程:就是ip相同,但是把不同端口绑定到指定业务
这个更简单
[root@123456 conf.d]# vim game1.conf
server {
listen 192.168.74.128:80;
[root@123456 conf.d]# vim game2.conf
server {
listen 192.168.74.128:81;
[root@123456 conf.d]# systemctl restart nginx
[root@123456 conf.d]# netstat -tnulp|grep nginx
tcp 0 0 192.168.74.128:80 0.0.0.0:* LISTEN 11884/nginx: master
tcp 0 0 192.168.74.128:81 0.0.0.0:* LISTEN 11884/nginx: master
浏览器按照ip:端口方式访问
方法3.多域名配置多业务
这个比较常用,节省ip资源,减少网络配置
流程:就是一个ip对应多个域名,每个域名又对应一个conf,这样访问不同的域名时就可跳转到指定的业务
[root@123456 conf.d]# vim game1.conf
server {
listen 192.168.74.128:80;
server_name game1.com;
#access_log /var/log/nginx/host.access.log main;
location / {
root /code/;
index index.html index.htm;
}
[root@123456 conf.d]# vim game2.conf
server {
listen 192.168.74.128:80;
server_name game2.com;
#access_log /var/log/nginx/host.access.log main;
location / {
root /code1/;
index index.html index.htm;
}
[root@123456 conf.d]# systemctl restart nginx
[root@123456 conf.d]# netstat -tnulp|grep nginx
tcp 0 0 192.168.74.128:80 0.0.0.0:* LISTEN 12582/nginx: master
做好hosts域名解析后,即可访问
这是我的个人学习笔记,主要用于记录自己对知识点的理解和梳理。由于目前仍在学习探索阶段,内容中难免存在理解偏差或表述疏漏,恳请各位大佬不吝赐教,多提宝贵意见~ 若有不同看法,欢迎理性交流探讨,感谢包容与指正!