Nginx配置文件详解

###进入nginx 目录
cd /usr/local/nginx

###递归显示 2 级目录
tree -L 2 ./

一、conf 目录

 conf 目录:存放nginx 配置文件的目录

  •  fastcgi.conf:存放fastcgi 相关的配置
  • fastcgi.conf.default:fastcgi.conf 的原始备份文件,用于还原
  • fastcgi_params:fastcgi 相关参数文件
  • fastcgi_params.default:fastcgi_params 的原始备份文件,用于还原
  • koi-utf:编码转换映射文件
  • koi-win:编码转换映射文件
  • mime.types:存放媒体资源的类型,例如:xml、html、css
  • mime.types.default:mime.types 的原始备份文件,用于还原
  • nginx.conf:nginx 默认主配置文件
  • nginx.conf.default:nginx.conf 的原始备份文件,用于还原
  • scgi_params:与fastcgi_params一样,传递哪些服务器的变量
  • scgi_params.default:scgi_params 的原始备份文件,用于还原
  • uwsgi_params:服务器和服务端应用程序的通信协议,规定了怎么把请求转发给应用程序和返回
  • uwsgi_params.default:uwsgi_params 的原始备份文件,用于还原
  • win-utf:编码转换映射文件

二、fastcgi_temp 目录

  • fastcgi_temp:临时存放fastcgi 数据的目录

三、html 目录

html:网站的根目录,访问的网站页面信息都存放在这个目录下

  •  50x.html:网站的错误页面,所以500的错误页面
  • idnex.html:默认首页
  • index.php:php 的默认首页

HTTP响应状态码

  • 100-199:指客户端的响应动作
  • 200-299:响应成功
  • 300-399:文件或者目录发生了移动
  • 400-499:客户端的错误
  • 500-599:服务端的错误

常见的响应状态码

  • 200:OK,访问成功
  • 301:Moved,永久跳转,请求的页面已经被重新定义到其他位置
  • 403:Forbidden,禁止访问,权限不够,检测网站根目录的权限
  • 404:Not Found,未找到页面,路径不对、文件名不对、服务器上没有这个路径或者文件
  • 500:Iternal server error,开启selinux 可能会导致
  • 502:Bad Gateway,代理服务器有问题,nginx+php 转发的时候可能会出,转发的后端服务器没有找到,或者转发不对都可能导致这个问题
  • 504:Gateway timeout,网关代理服务器请求后端服务器的时候,没有在指定的范围内完成解析,就会报错504

四、logs 目录

  •  access.log:访问日志,记录访问 nginx 页面的信息,包括IP地址、谁来访问的、访问的页面、响应状态码等
  • error.log:错误日志,nginx 的错误文件,启动错误,就看这个日志
  • nginx.pid:nginx 的 pid 进程号

五、proxy_temp 目录

  • proxy_temp:代理的数据临时存放的目录 

六、sbin 目录

  •  nginx:nginx启动命令

nginx.conf 配置文件详解

  • nginx主配置区域,对应nginx 核心模块的功能

###main区域,主配置区域,全局的

user  nginx;
worker_processes  1;    ###与CPU核数相等或者2倍

error_log  logs/error.log;    ###错误日志
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;    ###nginx pid 文件位置



###events区域

events {
    worker_connections  1024;    ###一个进程可以产生多少个工作的连接,nginx的最大并发访问量
    use epoll;    ###使用epoll 模型,异步IO 处理模型,epoll 模型没有1024 的限制,并发发访问量特别快
}

nginx核心模块:

  1. ngx_http_core_module

  • nginx 核心模块配置参数

    http {
        include       mime.types;    ###包含媒体资源类型的文件,调用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"';
    
        access_log  logs/access.log  main;
    
        sendfile        on;    ###发送文件的
        #tcp_nopush     on;    
    
        #keepalive_timeout  0;
        keepalive_timeout  65;    ###连接保持,占用系统资源少,访问速度快
    
        gzip  on;
    
        ###每一个server 段都是一个虚拟主机
        server {
            listen       80;    ###监听80 端口,可以做基于端口的虚拟主机,listen 后面 IP加端口就是基于IP的虚拟主机
            server_name  localhost;    ###主机名,可以做基于域名的虚拟主机
    
            charset utf-8;
    
            #access_log  logs/host.access.log  main;    ###定义虚拟主机的访问日志
    
            ###localtion 匹配段,用于匹配
            location / {
                root   html;
                index  index.php index.html index.htm;
            }
    
            #error_page  404              /404.html;    ###跳转404页面
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;    ###错误页面重定义
            location = /50x.html {
                root   html;
            }
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            location ~ \.php$ {
                root           html;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
                include        fastcgi.conf;
            }
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            #    deny  all;
            #}
        }
    
    
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    

  • log_format:指定日志格式    
  • $remote_addr:远程客户端的IP地址
  • $remote_user:远程客户端用户名称,用于记录浏览者进行身份验证时提供的名字,如登录百度的用户名scq2099yt,如果没有登录就是空白
  • $time_local: 访问的时间与时区,比如18/Jul/2012:17:00:01 +0800,时间信息最后的"+0800"表示服务器所处时区位于UTC之后的8小时
  • $request:请求的URI和HTTP协议,这是整个PV日志记录中最有用的信息,记录服务器收到一个什么样的请求
  • $status:记录请求返回的http状态码,比如成功是200
  • $body_bytes_sent:发送给客户端的文件主体内容的大小,比如899,可以将日志每条记录中的这个值累加起来以粗略估计服务器吞吐量
  • $http_referer:记录从哪个页面链接访问过来的(请求头Referer的内容 )
  • $http_user_agent:客户端浏览器信息(请求头User-Agent的内容 )
  • $http_x_forwarded_for:客户端的真实ip,通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加 x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端请求的服务器地址。

建立多个web站点,可以使用虚拟主机

  • 基于域名(使用较多)
  • 基于IP
  • 基于断端口

 localtion [ = | ~ | ~* | ^~ ] [URL]

  • ~:区分大小写
  • ~*:不区分大小写
  • !~:取反
  • ^~:不使用正则表达式(nginx 使用的是perl 正则)

URL:唯一资源定位符

 location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.ph  p;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf; 

}

localtion 字段含义:
    · "~" 区分大小写
    · "\.php$" 转义字符含义
    · "." 在正则里面表示任意所有,通过 \ 转义含义
    · "$" 以 ".php" 结尾,如果匹配到以".php" 结尾的文件,进入到 "html" 目录去找它,这个请求通过 "fastcgi_pass" 转交给本地的9000 端口,fastcgi默认的首页是 index.php

  • 11
    点赞
  • 83
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Nginx 配置文件是一个文本文件,通常位于 `/etc/nginx/` 目录下,主要包括以下几个部分: 1. 全局块:配置影响 nginx 全局的指令,一般有运行 nginx 的用户组、nginx 进程 pid 存放路径、日志存放路径和类型以及配置文件引入等。 2. events 块: 配置影响 nginx 服务器或与用户的网络连接,常用于设置连接超时时间、最大连接数等。 3. http 块:http 块中定义的配置指令用于处理 Web 请求,主要包括了 MIME 类型、字符集、缓存、请求限制等。 4. server 块:配置虚拟主机的相关参数,一个 http 块中可以包含多个 server 块,每个 server 块就相当于一个虚拟主机,用于处理来自客户端的请求。 一个简单的 Nginx 配置文件示例如下: ``` user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; server { listen 80; server_name example.com; location / { root /usr/share/nginx/html; index index.html index.htm; } } } ``` 这个配置文件包含全局块和一个 server 块,其中: - `user` 指定 Nginx 运行的用户; - `worker_processes` 指定 Nginx 启动的 worker 进程数; - `error_log` 指定错误日志文件路径和级别; - `pid` 指定 Nginx 进程 ID 存放路径; - `events` 块中 `worker_connections` 指定每个 worker 进程最大连接数; - `http` 块中 `include` 指定 MIME 类型配置文件路径; - `default_type` 指定默认 MIME 类型; - `access_log` 指定访问日志文件路径和格式; - `sendfile` 指定是否使用 sendfile 函数传输文件; - `keepalive_timeout` 指定 keep-alive 连接超时时间; - `server` 块中 `listen` 指定监听端口和协议; - `server_name` 指定虚拟主机域名; - `location` 指定请求 URL 和对应的文件路径。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值