初探Nginx(一)

前言

老实说,从Android开发转到后端开发,有些基础概念仍是比较模糊的,特别是对一些框架的熟悉。其中,Nginx算一个,因而忽然有了想搞懂Nginx的冲动。。。php

Nginx是什么?

  • 按照惯例,来波官方定义。

nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server, originally written by Igor Sysoev. For a long time, it has been running on many heavily loaded Russian sites including Yandex, Mail.Ru, VK, and Rambler. According to Netcraft, nginx served or proxied 26.34% busiest sites in June 2019. Here are some of the success stories: Dropbox, Netflix, Wordpress.com, FastMail.FM.html

  • 释义:Nginx是一个HTTP服务器、反向代理服务器、邮件代理服务器、TCP/UDP代理服务器。java

  • 说到这里,就会有个疑问,和咱们一般使用的Tomcat有何区别?nginx

TomcatNginx的应用场景不一样。Nginx是开源、高性能的HTTP服务器,而Tomcat更可能是一种容器,做为Web服务器处理Java Servlet、JSP等。正则表达式

nginx安装

## 安装指令
 brew install nginx
 ## 安装目录
 cd /usr/local/etc/nginx
复制代码

nginx.conf

Nginx很重要的一环就是配置文件,学习配置文件的格式以及如何使用每一个配置是基础。apache

全局配置参数

#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 {
    # we’re on a Solaris-based system and have determined that nginx
	# will stop responding to new requests over time with the default
	# connection-processing mechanism, so we switch to the second-best
	use /dev/poll;
	# the product of this number and the number of worker_processes
	# indicates how many simultaneous connections per IP:port pair are accepted
	worker_connections 2048;
}
复制代码
  • user:使用这个参数来配置worker进程的用户和组。若是忽略group,那么group的名字等于该参数指定用户的用户组。
  • worker_processes:指定worker进程启动的数量。这些进程用于处理客户的全部链接。选择 一个正确的数量取决于服务器环境、磁盘子系统和网络基础设施。 一个好的经验法则是设置该参数的值与CPU绑定的负载处理器核心的数量相同,并用 1.5~2之间的数乘以这个数做为I/O密集型负载。
  • error_log:是全部错误写入的文件。若是在其余区段中没有设置其余的error_log,那么这个日志文件将会记录全部的错误。该指令的第二个参数指定了被记录错误的级别( debug、 info、 notice、 warn、 error、 crit、 alert、emerg)。注意,debug级别的错误只有在编译时配置了--with-debug 选项才可使用。
  • pid:设置记录主进程ID的文件,这个配置将会覆盖编译时的默认配置。
  • use:该指令用于指示使用什么样的链接方法。这个配置将会覆盖编译时的默认配置,若是配置该指令,那么须要一个events 区段。 一般不须要覆盖,除非是当编译时的默认值随着时间的推移产生错误时才须要被覆盖设置。
  • worker_connections:该指令配置一个工做进程可以接受并发链接的最大数。这个链接包括客户链接和向上游服务器的链接,但并不限于此。这对于反向代理服务器尤其重要,为了达到这个并发性链接数量,须要在操做系统层面进行一些额外调整。

HTTP server部分

http {
	## include文件
    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;

	## 文件I/O指令,该指令使用sendfile(2)直接复制数据从一个到另外一个文件描述符
    sendfile        on;
    ## 仅依赖于sendfile使用,Nginx在一个数据包中尝试发送响应头以及在数据包中发送一个完整的文件
    #tcp_nopush on;

	## 该指令指定keep-alive链接持续多久。
    #keepalive_timeout 0;
    keepalive_timeout  65;

	## 开启gzip压缩
    #gzip on;
}
复制代码

虚拟服务器部分

  • 描述的是一组根据不一样的server_name指令逻辑分割的资源。
server {
		
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log logs/host.access.log main;

		## location 指令能够用在虚拟服务器 server 部分,井且意味着提供来自客户端的 URI 或 者内部重定向访问。
        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;
        #}
    }
复制代码
  • 对于一个特定的请求,肯定哪些虚拟服务器提供该请求的服务时,Nginx应该遵循下面的逻辑。
  1. 匹配 IP 地址和 listen 指令指定的端口。
  2. 将 Host头字段做为一个字符串匹配 server_name指令。
  3. 将 Host 头字段与 server_name 指令值字符串的开始部分作匹配 。
  4. 将 Host 头字段与 server_name 指令值字符串的结尾部分作匹配 。
  5. 将 Host 头字段与 sever_name 指令值进行正则表达式匹配 。
  6. 若是全部 Host 头匹配失败,那么将会转向 listen 指令标记的 default_server。
  7. 若是全部的 Host头匹配失败,而且没有 default_sever,那么将会转向第一个 server 的 listen指令,以知足第1步。

结语

能够看出,Nginx的配置是模块化的,全局配置负责各个方面,HTTP Server、虚拟服务器则分模块配置,每一个server_name单独生效。后端

参考文献

一、www.nginx.cn/doc/general… 二、精通Nginx(第二版) 三、examples.javacodegeeks.com/enterprise-…tomcat

相关文章
相关标签/搜索