编程技术分享平台

网站首页 > 技术教程 正文

Ngnix配置文件nginx.conf的文件结构

xnh888 2024-10-10 13:14:42 技术教程 27 ℃ 0 评论

日常,在配备ngnix文件的时候,最经常配置的文件之一就是ngnix.conf这个文件了,这一节简单学习下nginx.conf这个文件

使用pwd可以查看ngnix的文件

想要查看nginx.conf的命令,首先找到nginx.conf这个命令所在的位置

切换到nginx.conf的路径之后,使用cat nginx.conf命令可以查看conf命令的相关内容

worker_processes  1;
 events {
 worker_connections  1024;
 }
 http {
    include mime.types;      
    default_type application/octet-stream;
    sendfile on;       
    keepalive_timeout  65; 
 server {
 listen  80;     
 server_name  localhost;
 location / {
 root html;  
 index  index.html index.htm;
 }
 error_page 500 502 503 504  /50x.html;  
 location = /50x.html {
 root html;  


}
 }
  }

在events里和http里的内容称为全局块

这里的worker_processes就是全局块

events{

worker_connections 1024的意思是什么?

} events块主要配置与用户的网络连接相关的内容

ngnix代理,日志缓存等内容在http块中都可以进行配置

在http块中的是server块,一个http块可以配置多个server块

平常我们配置http请求的时候,经常会碰到在http块的

server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } 这样的内容,listen 80; server_name localhost的意思是监听localhost80端口的内容

拦截完localhost:80端口的内容,交给谁来处理??它会匹配/的内容,然后获取对应的资源,交还给用户

error_page的意思是 500,502,503,504如果遇到对应的错误,就会返回对应的页面

50x.html是访问失败设置的页面

根据对应的配置/,跳到成功之后,展现的页面

如果访问/welcome.html一个失败的页面,会报错,这里报错的原因是没有在Ngnix中找到对应的资源,如果想要在404中也跳转到50x.html这个页面

在error_page文件,将404添加上,就可以返回50x.html文件

修改完配置之后,必须重启一下配置

cd ../sbin/

./nginx -s reload

再出现404的问题之后,就会跳转到50x.html页面了

也可以使用vim ../html/50x.html进行相应修改

设置对应的html页面,展示错误发生时,设置成自己想要的报错页面

小结:

nginx.conf配置文件中默认有三大块:全局块、events块、http块 http块中可以配置多个server块,每个server块又可以配置多个location块

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表