初探Nginx(一)

675 阅读4分钟

前言

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

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.

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

  • 说到这里,就会有个疑问,和我们通常使用的Tomcat有何区别?

TomcatNginx的应用场景不同。Nginx是开源、高性能的HTTP服务器,而Tomcat更多是一种容器,作为Web服务器处理Java Servlet、JSP等。

nginx安装

 ## 安装指令
 brew install nginx
 ## 安装目录
 cd /usr/local/etc/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 {
    # 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单独生效。

参考文献

1、www.nginx.cn/doc/general… 2、精通Nginx(第二版) 3、examples.javacodegeeks.com/enterprise-…