Nginx topic (a) Introduction -----

Nginx Profile

Apache

Apache is still the highest long-footprint web server, according to the latest statistics, the market share is currently about 50%. The main advantage is that a server is a static resource Http appeared earlier, but it is also open source. Therefore, solutions and technical support in the market are more mature. Apache modules supported by very rich.

Nginx

Nginx is written in Russian and a high-performance HTTP reverse proxy server, in the case of high concurrent connections, it can support up to 50,000 concurrent response connections, but the memory, CPU and other system resource consumption is very low, running very stable. Currently Nginx in many large domestic enterprises have applied, according to the latest statistics, Nginx's market share has reached about 33% of the. The Apache's market share although still the highest, but it is declining. Nginx while the momentum is obvious. Nginx select the reason is simple: First, it can support 5W high concurrent connections; second, less memory consumption; third, low cost, if F5, NetScaler load balancing and other hardware devices, it requires a large hundreds of thousands. And Nginx is open source, and can be used free for commercial use

 

Commonly used Web server Introduction 

apache、Nginx、tomcat、weblogic、iis、jboss、websphere、 jetty、netty、lighttpd、glassfish

 

 

1, Chrome string obtained html

2, html text server, over the network to chrome

3, server, html string to generate their own temporary return

4, it traditional industries: telecommunications / Financial weblogic / jboss / wesphere

5、jboos= ejb + tomcat

6, IIS, c # in.

7, nginx and apache, a web service level

The role of architecture

System architecture describes the role nginx (Gateway entry) are summarized below:

1, routing function (corresponding to the micro Services): the domain name / path routing backend server

2, the load functions (corresponding to the availability of high concurrency): cluster load of the backend server

3, static server (much higher than the performance tomcat): In mvvm mode, acts as a file reading duties

总结:实际使用中,这三项功用,会混合使用。比如先分离动静,再路由服务,再负载机器

正向代理与反向代理

1、代理:客户端自己请求出现困难。客户请了一个代理,来代自己做事,就叫代理。

    比如代理律师,代购,政府机关办事的代理人等等。

2、反向代理

是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。

NGINX安装-----源码编译方式

在本机安装了vmware,然后在虚拟机上安装nginx-----安装目录/usr/local/src

1、 wget http://nginx.org/download/nginx-1.9.0.tar.gz

 

 

这就下载好了。

2、解压

tar -zxvf nginx-1.9.0.tar.gz

.tar.gz代表打包、压缩文件

3、可能需要

安装make:

yum -y install autoconf automake make

安装g++: 

yum -y install gcc gcc-c++

#一般系统中已经装了了make和g++,无须再装

yum -y install pcre pcre-devel
yum -y install zlib zlib-devel
yum install -y openssl openssl-devel

4、配置

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

#--prefix指定安装目录

#--with-http_ssl_module安装https模块

#creating objs/Makefile 代表编译成功

出现objs/Makefile代表编译成功

5、安装

make && make install

#make编译

#make install安装

安装完成之后查看nginx安装目录:

whereis nginx

 

 

最后的安装目录为: /usr/local/nginx

目录解释

  • Conf 配置文件
  • Html 静态网页文件
  • Logs 日志文件
  • Sbin 二进制程序

启停命令:

[root@hadoop1 nginx]# sbin/nginx
[root@hadoop1 nginx]# 
./nginx -c nginx.conf的文件。如果不指定,默认为NGINX_HOME/conf/nginx.conf

./nginx -s stop  停止

./nginx -s quit退出

./nginx -s reload 重新加载nginx.conf

启动nginx后,在浏览器访问vmware的ip地址:

 

 

先telnet看下端口是否能通 ,win10里面需要手动开启此功能:

 

不通的话设置下防火墙,如下:

/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/etc/rc.d/init.d/iptables save

 

重启防火墙:

/etc/init.d/iptables restart

然后再重新访问下浏览器:

发送信号的方式

kill -QUIT  进程号 安全停止

kil -TERM  进程号 立即停止

Nginx基础概念

Nginx会按需同时运行多个进程:

一个主进程(master)和几个工作进程(worker),配置了缓存时还会有缓存加载器进程(cache loader)和缓存管理器进程(cache manager)等。

所有进程均是仅含有一个线程,并主要通过“共享内存”的机制实现进程间通信。

主进程以root用户身份运行,而worker、cache loader和cache manager均应以非特权用户身份(user配置项)运行。

 

 

master

主进程主要完成如下工作:

1. 读取并验正配置信息;

2. 创建、绑定及关闭套接字;

3. 启动、终止及维护worker进程的个数;

4. 无须中止服务而重新配置工作特性;

5. 重新打开日志文件;

worker

worker进程主要完成的任务包括:

1. 接收、传入并处理来自客户端的连接;

2. 提供反向代理及过滤功能;

3. nginx任何能完成的其它任务;

nginx.conf配置文件结构

  • main(全局设置)
  • events设定nginx的工作模式及连接数上限
  • http 服务器相关属性
  • server(虚拟主机设置)
  • upstream(上游服务器设置,主要为反向代理、负载均衡相关配置)
  • location(URL匹配特定位置后的设置)
#user  nobody;  #主模块命令, 指定Nginx的worker进程运行用户以及用户组,默认由nobody账号运行。

worker_processes  1;#指定Nginx要开启的进程数。

worker_rlimit_nofile 100000;  #worker进程的最大打开文件数限制

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

use epoll;

worker_connections  1024;

}

/*

  以上这块配置代码是对nginx全局属性的配置。

  user :主模块命令, 指定Nginx的worker进程运行用户以及用户组,默认由nobody账号运行。       

  worker_processes: 指定Nginx要开启的进程数。

  error log:用来定义全局错设日志文件的路径和日志名称。日志输出级别有debug,info,notice,warn,error,crit 可供选择,其中debug输出日志最为详细,面crit(严重)输出日志最少。默认是error

  pid: 用来指定进程id的存储文件位置。

  event:设定nginx的工作模式及连接数上限,其中参数use用来指定nginx的工作模式(这里是epoll,epoll是多路复用IO(I/O Multiplexing)中的一种方式),nginx支持的工作模式有select ,poll,kqueue,epoll,rtsig,/dev/poll。其中select和poll都是标准的工作模式,kqueue和epoll是高效的工作模式,对于linux系统,epoll是首选。

  worker_connection:是设置nginx每个进程最大的连接数,默认是1024,所以nginx最大的连接数max_client=worker_processes * worker_connections。进程最大连接数受到系统最大打开文件数的限制,需要设置ulimit。

*/

#下面部分是nginx对http服务器相关属性的设置

http {

    include       mime.types;               主模块命令,对配置文件所包含文件的设定,减少主配置文件的复杂度,相当于把部分设置放在别的地方,然后在包含进来,保持主配置文件的简洁

    default_type  application/octet-stream; 默认文件类型,当文件类型未定义时候就使用这类设置的。

 

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '   指定nginx日志的格式

    #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

 

    #access_log  logs/access.log  main;

    sendfile        on;   开启高效文件传输模式(zero copy 方式),避免内核缓冲区数据和用户缓冲区数据之间的拷贝。

    #tcp_nopush     on;  开启TCP_NOPUSH套接字(sendfile开启时有用)

 

    #keepalive_timeout  0;   客户端连接超时时间

    keepalive_timeout  65;

 

    #gzip  on;             设置是否开启gzip模块

#下面是server段虚拟主机的配置

server {

        listen       80;   虚拟主机的服务端口

        server_name  localhost;   用来指定ip或者域名,多个域名用逗号分开

        #charset koi8-r;

        location / {        

               #地址匹配设置,支持正则匹配,也支持条件匹配,这里是默认请求地址,用户可以location命令对nginx进行动态和静态网页过滤处理

            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;

        }     

}

Nginx日志描述

通过访问日志,你可以得到用户地域来源、跳转来源、使用终端、某个URL访问量等相关信息;通过错误日志,你可以得到系统某个服务或server的性能瓶颈等。因此,将日志好好利用,你可以得到很多有价值的信息。

日志格式

打开nginx.conf配置文件:vi /usr/local/nginx/conf/nginx.conf

日志部分内容:

#access_log  logs/access.log  main;

日志生成的到Nginx根目录logs/access.log文件,默认使用“main”日志格式,也可以自定义格式。

默认“main”日志格式: 参数明细表:

查看日志命令tail -f /usr/local/nginx/logs/access.log

 

Guess you like

Origin www.cnblogs.com/alimayun/p/12360958.html