Day36nginx基础知识
nginx安装
角色 | 外网ip | 内网ip | 主机名 |
---|---|---|---|
web | eth0:10.0.0.7 | eth1:172.16.1.7 | web01 |
- [root@web01 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
在web01机器下载nginx
[root@web01 ~]# yum install nginx -y
nginx的配置文件
[root@web01 ~]# 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运行一个网站,新增一个server配置就是增加一个网站
[root@web01 conf.d]# 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
重启nginx
[root@web01 code]# systemctl restart nginx
准备好代码的位置
[root@web01 conf.d]# mkdir /code
[root@web01 conf.d]# cd /code/
上传代码
[root@web01 code]# git clone https://gitee.com/linex/battlecity.git
[root@web01 code]# mv battlecity/* ./
在增加一个网站
[root@web01 code2]# vim /etc/nginx/conf.d/html.oldxu.com.conf
server {
listen 80;
server_name html.oldxu.com;
location / {
root /code2;
index index.html;
}
}
语法检查
[root@web01 code2]# nginx -t
重启服务
[root@web01 code2]# systemctl restart nginx
创建代码存放位置
[root@web01 ~]# mkdir -p /code2
[root@web01 ~]# cd /code2
上传代码

配置域名劫持

在浏览器输入域名