一、nginx安装
源码安装 —不考虑 更新时使用
epel源安装 —配置乱 不推荐
官方源安装 —推荐
vim /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
yum install nginx
二、nginx配置文件分析
cat /etc/nginx/nginx.conf
user nginx; # Nginx进程的运行用户身份
worker_processes 1; # Nginx运行的worker进程数
error_log /var/log/nginx/error.log warn; # Nginx错误日志存放的路径
pid /var/run/nginx.pid; # Nginx进程运行的PID号
events {
worker_connections 1024; #每个worker进程能接受的最大连接数
use epoll;
}
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"';
#访问日志的路径 访问日志的存储格式是main格式, main格式在log_format中进行定义
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65; #超时时间
#gzip on;
include /etc/nginx/conf.d/*.conf; #包含 *.conf
#server<--定义网站
server {
listen 80; #监听80端口
server_name localhost; #网站的域名
location / { #匹配网站的uri
root /usr/share/nginx/html; #返回资源的具体路径
index index.html index.htm; #返回的具体资源名称
}
}
http <---负责请求与响应
server1 <---定义一个具体的网站
location <---根据用户请求的uri 进行匹配
location2
location3
server2
location <---根据用户请求的uri 进行匹配
location2
location3
server....
三、nginx 运行一个网站
gzip default.conf
压缩,注释
cat default.conf.gz
查看内容
1.vim game.oldxu.com.conf
server {
listen 80;
server_name game.oldxu.com;
location / {
root /code/;
index index.html;
}
}
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
systemctl restart nginx
2.准备好代码的仓库位置
mkdir /code
cd /code/
3.上传代码
git clone https://gitee.com/linex/battlecity.git
mv /battlecity/* ./
4.访问测试
配置hosts劫持
使用火狐、谷歌浏览器
四、如何新增一个网站
1.新增一个nginx的配置文件
[root@web01 code2]# vim /etc/nginx/conf.d/gd.oldxu.com.conf
server {
listen 80 ;
server_name gd.oldxu.com;
location / {
root /code2;
index index.html;
}
}
[root@web01 code2]# nginx -t [root@web01 code2]# systemctl restart nginx
2.创建代码存放路径
[root@web01 ~]# mkdir -p /code2 [root@web01 ~]# cd /code2
3.上传游戏
windos rz Mac系统 scp
4配置域名劫持