Nginx学习总结(一)

 

一、基本概念

1.什么是Nginx?

     Nginx是一个高性能的web服务器和反向代理服务器,特点是占用内存少,并发能力强;事实上Nginx的并发能力确实在同类型的网页服务器中表现良好;

     Nginx专为性能优化而开发,性能是其主要的考量;实现上非常注重效率,经得住高并发的考验,有报告表名能支持高达50000个并发连接数

2.反向代理

(1)正向代理

      在客户端(浏览器)中需要配置代理服务器,通过代理服务器进行互联网访问

(2)反向代理

客户端只需要将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据,再返回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器地址

3.负载均衡

单个服务器解决不了,我们增加服务器数量,然后将请求分发到各个服务器上,将原先请求集中到单个服务器的情况改为将请求分发到多个服务器上,将负载分发到不同的服务器上,这就是负载均衡

4.动静分离

为了加快网站的访问速度,可以吧动态页面和静态页面由不同服务器来解析,加快解析速度。降低原来单个服务器的压力。

二、Nginx的安装

1.安装Nginx

①安装pcre 依赖

wget http://downloads.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz

解压文件

执行 ./configure 完成后,回到pcre目录下执行 make,

在执行 make install

② 安装openssl

③ 安装zlib

yum -y install make zlib zlib -devel gcc-c++ libtool openssl openssl-devel

④安装Nginx

(1)把Nginx安装文件放入linux系统中去
(2) 解压压缩文件
(3) make && make install
安装成功以后,在usr多出一个文件夹 local/nginx,在nginx有sbin有启动脚本
(4)启动nginx(进入local/nginx/sbin 目录, 执行 ./nginx
(5)防火墙设置

查看开放的防火墙: firewall -cmd --list-all

设置开放的端口:firewall -cmd --add-service=http -permanent

sudu firewall -cmd --add-port=8002/tcp --permanent

重启防火墙:firewall -cmd --reload

三、nginx的常用命令

1.使用nginx操作命令前提条件:进入到nginx的目录下(usr/local/nginx/sbin)

命令说明 命令
查看nginx;版本号 ./nginx -v
启动 nginx ./nginx
关闭nginx /nginx -s stop
重新加载 nginx ./nginx -s reload

四、nginx配置文件

nginx配置文件有三部分组成

(1) 全局块

从配置文件开始到events块之间的内容,主要会设置一些影响nginx服务器整体运行的配置命令,主要包括配置运行Nginx服务器的用户组,允许生成的 work_process数,进程PID存放路径,日志存放路径和类型一级配置文件的 引入等。

(2) events块

events块涉及的指令主要影响Nginx服务器与用户的网络连接

比如 worker_connections 1024; 支持的最大连接数

1  events {
2   worker_connections 1024;
3   }

(2) http块

  1 http {
  2   include mime.types;
  3   default_type application/octet-stream;
  4   5   #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  6   # '$status $body_bytes_sent "$http_referer" '
  7   # '"$http_user_agent" "$http_x_forwarded_for"';
  8   9   #access_log logs/access.log main;
 10  11   sendfile on;
 12   #tcp_nopush on;
 13   proxy_connect_timeout 5;
 14   proxy_read_timeout 60;
 15   proxy_send_timeout 60;
 16   proxy_buffer_size 32k;
 17   proxy_buffers 4 128k;
 18   proxy_busy_buffers_size 128k;
 19   proxy_temp_file_write_size 128k;
 20   client_max_body_size 128m;
 21   ignore_invalid_headers on;
 22  23  24   #keepalive_timeout 0;
 25  26    
 27  28  29   keepalive_timeout 65;
 30  31   #gzip on;
 32  33   upstream XXXXX {
 34   server IP地址:端口号;
 35  36   }
 37  38  39   server {
 40   listen 90;
 41   server_name localhost;
 42  43   #charset koi8-r;
 44  45   #access_log logs/host.access.log main;
 46  47  48   location / { 
 49   # these two lines here
 50   proxy_http_version 1.1;
 51   proxy_set_header Connection "";
 52   proxy_pass https://域名/;
 53   }
 54  55   location /ZZZZZ/ {
 56   proxy_pass http://XXXXX;
 57   }
 58  59   #error_page 404 /404.html;
 60  61   # redirect server error pages to the static page /50x.html
 62   #
 63   error_page 500 502 503 504 /50x.html;
 64   location = /50x.html {
 65   root html;
 66   }
 67  68  69   }
 70  71   server {
 72   listen 88;
 73   server_name localhost;
 74  75   #charset koi8-r;
 76  77   #access_log logs/host.access.log main;
 78  79  80   location / { 
 81   # these two lines here
 82   proxy_http_version 1.1;
 83   proxy_set_header Connection "";
 84   proxy_pass https://域名/;
 85   }
 86  87   location /ZZZZZ/ {
 88   proxy_pass http://XXXXX;
 89   }
 90  91   #error_page 404 /404.html;
 92  93   # redirect server error pages to the static page /50x.html
 94   #
 95   error_page 500 502 503 504 /50x.html;
 96   location = /50x.html {
 97   root html;
 98   }
 99 100 101   }
102 103   server {
104   listen 95;
105   server_name localhost;
106 107   #charset koi8-r;
108 109   #access_log logs/host.access.log main;
110 111 112   location / { 
113   # these two lines here
114   proxy_http_version 1.1;
115   proxy_set_header Connection "";
116   proxy_pass https://域名/;
117   } 
118   #error_page 404 /404.html;
119 120   # redirect server error pages to the static page /50x.html
121   #
122   error_page 500 502 503 504 /50x.html;
123   location = /50x.html {
124   root html;
125   }
126 127 128   }
129 130    
131 132   }

Nginx最频繁配置的地方,代理、缓存和日志定义等 绝大多数功能和第三方模块的配置都在这里

①http全局块

http全局块配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链请求数上限等。

② server块

这块和虚拟主机 相关,虚拟主机从用户侧看,和一台独立额硬件主机是完全一样的,该技术的产生是为了节省互联网服务器硬件成本。

每个http块可以包括多个server块,而每个server块相当于一个主机

每个server块也分为全局server块,以及可以同时包括多个location块

猜你喜欢

转载自www.cnblogs.com/ft-greate/p/12206416.html