#Nginx配置文件
Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块形式组织的。一般,每个区块以一对大{}来表开始与结束
1.CoreModule 核心模块 全局配置
2.EventModule 事件驱动模块 全局
3.HttpCoreModule http内核模块 局部配置
需了解拓展项
CoreModule层下可以有Event、HTTP
HTTP模块层允许有多个Server层,Server主要用于配置多个网站
Server层又允许有多个Location,Location主要用于定义网站访问路径
基本格式:
http{
server{
listten 80
server_name www.test.com
location / {
root /home/www
index index.html
}
}
server {
linsten 80
server_name xxx.test.com
localtion {
root /home/www1
index index.php
}
}
}
CoreModule核心模块
user www; #nginx进程所使用的用户
worker_processes 1; #nginx运行的work进程数量(建议与CPU数量一致火auto)
error_log /log/nginx/error.log $nginx错误日志存放路径
pid /var/run/nginx.pid #nginx服务运行后产生的pid进程号
events事件模块
events {
worker_connections 1024;//每个worker进程支持的最大连接数
use epool; //事件驱动模型,epoll默认
}
http内核模块
//公共的配置定义在http{}
http{ //http层开始
...、
//使用Server配置网站,每个Server{}代表一个网站(简称虚拟主机)
server{
listen 80; //监听端口,默认80
server_name localhost; //提供服务的域名或主机名
acces_log host.access.log //访问日志
//控制网站访问路径
location / {
root /home/www //存放网站代码路径
index index.html index.htm index.php; //服务器返回的默认页面文件
}
//指定错误代码,统一定于错误页面,错误代码重定向到新的localtion
error_page 500 502 503 504 /50x.html;
}
egrep -v '^$|^.*#' default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Nginx配置网站
目的:当我们访问www.test.com的是时候,访问/home/www里的页面代码
1.在/etc/nginx/conf.d下新建配置文件,要求一定是.conf结尾,然后将默认配置关闭
cd /etc/nginx/conf.d
mv default.conf default.conf.back
vim test.conf
cat test.conf
server{
listen 80;
server_name www.test.com;
location /{
root /usr/local/www/htdocs/admin;
index index.php index.html index.htm;
}
}
配置完成语法测试
[root@iZ0qjhhu2rrhmpZ 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
2.获取网页代码
mkdir -p /user/local/www{test,test1}
正确配置
3.重载或启动nginx
systemctl reload nginx(推荐)
systemctl start nginx
nginx -s reload
通过域名访问。修改windows本地hosts文件