1、nginx官方仓库安装
配置官网仓库配置nginx
打开官方网站https://nginx.org,下载对应的安装包进行下载安装(我对应的是redhat)
[root@web02 ~]#vim /etc/yum.repos.d/nginx.repo #配置官方yum源位置
[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
2、安装nginx服务
[root@web02 ~]#yum -y install nginx #安装软件
[root@web02 ~]#nginx -v #查看版本
nginx version: nginx/1.24.0
3、配置nginx服务
[root@web02 ~]#cat /etc/nginx/nginx.conf #主配置文件
#核心区块
user nginx; #启动nginx的虚拟用户,默认已经存在
worker_processes auto; #启动子进程工作数量,auto是以cpu的内核数为准
error_log /var/log/nginx/error.log notice; #错误日志所在的位置,notice级别默认
pid /var/run/nginx.pid; #进程pid所存放的目录,
#事件模块
events {
worker_connections 25532; #子进程最大连接数,调大,tcp连接work数量
}
#http模块
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; #长链接的超时时间
#gzip on; #传输的时候是否开启压缩,默认开启
include /etc/nginx/conf.d/*.conf; #包含了conf.d目录下所有的.conf,将*.conf内容移动到了当前的文件中
}
主机配置文件(业务配置)
vim /etc/nginx.conf/game.conf
server {
listen 80;
server_name www.game.com;
location / { #匹配规则
root /code/; #如果用户访问的是/目录,则访问的是让用户去/code目录下取文件
index index.html index.htm; #默认访问给用户访问的主页面
}
}
[root@web02 conf.d]#ll
总用量 8
-rw-r--r-- 1 root root 1072 4月 12 2023 default.conf
-rw-r--r-- 1 root root 112 4月 15 22:09 game.conf
[root@web02 conf.d]#cat game.conf
server {
listen 80;
server_name www.game.com;
location / {
root /code/;
index index.html index.htm;
}
}
[root@web02 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
[root@web02 conf.d]#systemctl restart nginx #重启生效
deMacBook-Pro:~ root# vim /etc/hosts #修改客户端(mac)的hosts文件
10.0.0.8 www.game.com
[root@web02 conf.d]#mkdir /code #创建制定目录
#没有目录报404错误
#有目录但是没有文件报403错误
#304指的是浏览器缓存
#200指的是访问正常
[root@web02 conf.d]#echo Best the world! > /code/index.html #写入测试
4、启动服务并且加入开机自动
[root@web02 ~]#systemctl start nginx
[root@web02 ~]#systemctl enable nginx
5、查看nginx是否启动成功
[root@web02 ~]#netstat -utnlp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4387/nginx: master #主进程
[root@web02 ~]#ps -aux | grep nginx
root 4387 0.0 0.1 49072 1004 ? Ss 21:06 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 4388 0.0 0.1 49460 1900 ? S 21:06 0:00 nginx: worker process
root 4488 0.0 0.0 112824 980 pts/0 R+ 21:08 0:00 grep --color=auto nginx
[root@web02 nginx]#ss | grep ES | grep 80
u_str ESTAB 0 0 * 18038 * 18039
u_str ESTAB 0 0 /run/systemd/journal/stdout 18039 * 18038
[root@web02 nginx]# ss -an | grep 80
u_str ESTAB 0 0 * 18038 * 18039
u_dgr UNCONN 0 0 * 21580 * 9000
u_str ESTAB 0 0 /run/systemd/journal/stdout 18039 * 18038
tcp LISTEN 0 128 *:80 *:*
tcp ESTAB 0 0 10.0.0.8:80 10.0.0.1:64539
tcp ESTAB 0 0 10.0.0.8:80 10.0.0.1:64540
6、配置文件
[root@web02 nginx]#cd /etc/nginx/
[root@web02 nginx]#ll
总用量 24
drwxr-xr-x 2 root root 26 4月 15 20:59 conf.d #站点配置文件
-rw-r--r-- 1 root root 1007 4月 12 2023 fastcgi_params #使用php的参数选项
-rw-r--r-- 1 root root 5349 4月 12 2023 mime.types #服务器支持的代码类型、网站支持类型
lrwxrwxrwx 1 root root 29 4月 15 20:59 modules -> ../../usr/lib64/nginx/modules #指向模块位置
-rw-r--r-- 1 root root 648 4月 12 2023 nginx.conf #主配置文件
-rw-r--r-- 1 root root 636 4月 12 2023 scgi_params #代理相关 py相关
-rw-r--r-- 1 root root 664 4月 12 2023 uwsgi_params #也是代理相关
7、放置小游戏再次测试
[root@web02 code]#unzip xbw.zip #解压缩小游戏
-rw-r--r-- 1 root root 28032 May 24 2021 bgm.mp3
drwxr-xr-x 2 root root 23 May 24 2021 css
drwxr-xr-x 2 root root 23 May 24 2021 images
-rw-r--r-- 1 root root 8956 May 24 2021 index.html
drwxr-xr-x 2 root root 213 May 24 2021 js
drwxr-xr-x 2 root root 4096 May 24 2021 roms
-rw-r--r-- 1 root root 811 May 24 2021 shuoming.html
-rw------- 1 root root 7902976 Apr 15 22:35 xbw.zip