Nginx 基本原理与最小配置

简介: Nginx 基本原理与最小配置

文章和代码已经归档至【Github仓库:https://github.com/timerring/front-end-tutorial 】或者公众号【AIShareLab】回复 nginx 也可获取。

目录结构

进入Nginx的主目录有如下文件夹

client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp

其中以_temp结尾的文件夹是用来存放运行过程中的临时文件了。

其他主要的文件夹是:

  • conf:用来存放配置文件相关
  • html:用来存放静态文件的默认目录 html、css等
  • sbin:nginx的主程序
  • logs:存储各种日志,例如access记录访问的相关记录,error记录报错的记录,nginx.pid记录服务的pid号,即进程id号。

基本运行原理

一共有多个进程,其中有一个主进程Master负责读取,校验配置文件。

而子进程Worker则是相应对应的访问等请求。

Nginx配置与应用场景

首先重点是Nginx的配置文件 nginx.conf ,其中有很大一部分的注释配置,这里先关注nginx所需的最小配置。

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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 {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # 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_params;
        #}

        # 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;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

最小配置

worker_processes

worker_processes 1; 默认为1,表示开启一个业务进程,对应服务器的物理cpu的内核数,一个cpu内核对应一个worker_processes。当然也可以加大worker_processes 个数,但是对于同一个cpu来说需要调度,效率反而下降了。

events模块下

worker_connections

worker_connections 1024; 单个业务进程可接受连接数。

http模块下

include mime.types;

include mime.types; 引入http mime类型,加在http头中为浏览器指明应该解析的格式。

例如:

  • application/octet-stream bin exe dll;

    就是指明浏览器,以数据流的方式解析exe等类型,即下载下来。

  • image/jpeg jpeg jpg;

    则是直接让浏览器以图像的方式展示。

default_type application/octet-stream;

如果mime类型没匹配上,默认使用二进制流的方式传输。

请求信息让操作系统收到,操作系统的网络接口转发请求到Nginx(请求前绑定注册端口)。如果关闭 sendfile on; 则Nginx先根据配置文件读取SSD上的文件到应用程序中,然后再发送到操作系统的网络接口(即网卡的驱动程序),这个过程会经过调度,网卡的缓存以及内核的缓存,层层缓存复制。

但是如果开启了sendfile on; ,则是直接发送信号,让网络接口读取文件。

keepalive_timeout 65;

保持连接超时时间,反向代理阶段会详解。

server模块下

nginx可以配置多个server,一个server就是一个主机。

虚拟主机配置

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;
    }
}
listen 80;

每个主机的监听端口号不同,相互不干扰。这每一个主机也成为虚拟主机(vhost)。

server_name localhost;

主机名(必须写能解析的主机名,例如在本机的host文件中定义了localhost是127.0.0.1。或者改为域名也可以)

location

匹配路径,用来匹配uri。通常完整的链接叫做url:http://123.com/456/index.html 而uri是指/456/index.html这一部分。

root html;

文件根目录,这里是相对路径。

index index.html index.htm;

默认页名称,这里index就是index.html或者index.htm。

error_page 500 502 503 504 /50x.html;

报错编码对应页面,通常返回500等错误,会自动跳转到http://123.com/50x.html 页面。

而如果没有这个页面,根据下面的逻辑,会自动跳转到root(即html目录)中找该页面。

    location = /50x.html {
        root html;
    }

Nginx拿到IP地址,从DNS服务器,发起TCP/IP,TCP/IP协议只能传递一些二进制的数据,这些数据以数据流的形式发送给目标服务器。HTTP协议在TCP/IP协议之上,底层的协议TCP/IP里不带约束。但是HTTP协议实现了终止符,请求的数据报文究竟有多长等等信息。另外一种协议https协议,是在http协议的基础之上,额外增加了一层数据安全的这种保障。因为在上网的时候会经历很多的网关,像我们家里的路由器,还有小区网关,服务供应商网关,最后电信联通网关。从区一级的网关,再到市一级的网关,再到全国的,经过加密后安全性更好。

目录
相关文章
|
12天前
|
移动开发 前端开发 JavaScript
前端vue2、vue3去掉url路由“ # ”号——nginx配置(一)
前端vue2、vue3去掉url路由“ # ”号——nginx配置
41 0
|
12天前
|
JavaScript 前端开发 应用服务中间件
angular引入包、路由权限配置、打包问题与nginx配置问题(简单部署)
angular引入包、路由权限配置、打包问题与nginx配置问题(简单部署)
19 0
|
12天前
|
前端开发 JavaScript 应用服务中间件
前端vue2、vue3去掉url路由“ # ”号——nginx配置(二)
前端vue2、vue3去掉url路由“ # ”号——nginx配置
36 0
|
5天前
|
应用服务中间件 PHP nginx
php如何实现检测nginx配置的正确性
请确保在执行此操作时,PHP有足够的权限来执行Nginx命令和访问Nginx配置文件。另外,将上述代码嵌入到您的应用程序中时,要注意安全性,以防止潜在的命令注入攻击。
35 3
|
12天前
|
安全 应用服务中间件 网络安全
linux_nginx中添加ssl配置(open ssl)
linux_nginx中添加ssl配置(open ssl)
22 1
|
12天前
|
JSON JavaScript 前端开发
vue2_vite.config.js的proxy跨域配置和nginx配置代理有啥区别?
vue2_vite.config.js的proxy跨域配置和nginx配置代理有啥区别?
26 1
|
15天前
|
安全 应用服务中间件 网络安全
SSL原理、生成SSL密钥对、Nginx配置SSL
现在,你的Nginx虚拟主机应该已经配置了SSL,可以通过HTTPS安全访问。确保在生产环境中使用有效的SSL证书来保护通信的安全性。
27 0
|
Web App开发 应用服务中间件 nginx
Nginx 配置指令的执行顺序(学习笔记二十)
大多数 Nginx 新手都会频繁遇到这样一个困惑,那就是当同一个location配置块使用了多个 Nginx 模块的配置指令时,这些指令的执行顺序很可能会跟它们的书写顺序大相径庭。
1747 0
|
应用服务中间件 nginx
Nginx 配置指令的执行顺序(二)
我们前面已经知道,当 set 指令用在 location 配置块中时,都是在当前请求的 rewrite 阶段运行的。事实上,在此上下文中,ngx_rewrite 模块中的几乎全部指令,都运行在 rewrite 阶段,包括 Nginx 变量漫谈(二) 中介绍过的 rewrite 指令。
1260 0
|
缓存 监控 应用服务中间件
九爷带你了解 nginx 日志配置指令详解
nginx日志配置指令详解 日志对于统计排错来说非常有利的。 本文总结了nginx日志相关的配置如 access_log、log_format、open_log_file_cache、log_not_found、log_subrequest、rewrite_log、error_log。
1343 0