nginx配置文件

#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文件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值