发布于 

nginx入门

01 nginx简介

又称 engin x,是一个高性能的 HTTP和反向代理服务器

特点

  • 内存少,专为性能优化开发
  • 并发能力强,能经受高负载考验

02 nginx概念

正向和反向代理

正向代理

Nginx同样可以用作正向代理进行上网。
正向代理: 典型的就是VPN上网,需要在客户端配置代理服务器

反向代理

客户端对代理无感知,客户端无需做任何配置,
由反向代理服务器选择目标服务器,反向代理服务器和目标服务器就是一个服务器,
暴露代理服务器地址,隐藏目标服务器地址

负载均衡

单个服务器性能低,可以增加服务器的数量,将请求分发到各个服务器上,也就是负载均衡。

动静分离

为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器解析。

03 nginx的安装和使用

3.1 nginx的安装

3.2 nginx常用命令

前提条件: 必须要进入nginx目录。
具体目录位置:

  • /usr/local/nginx/sbin/

命令:

  • 查看版本号
    ./nginx -v
  • 查看进程
    ps -ef | grep nginx
  • 启动nginx
    ./nginx
  • 关闭nginx
    ./nginx -s stop
  • 重新加载nginx(特别是修改nginx.conf之后)
    ./nginx -s reload

3.3 nginx配置文件

配置文件位置: /usr/local/nginx/conf/nginx.conf

/usr/local/nginx/conf/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#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;
listen 8080;
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;
# }
#}

从开始到events块之间的内容,
影响nginx服务器的整体运行

1
2
3
4
5
6
7
8
9
#user  nobody;
worker_processes 1; # 表示nginx服务器并发处理的数量,越大表示处理并发量越多

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

#pid logs/nginx.pid;

影响Nginx服务器与用户网络的连接

1
2
3
events {
worker_connections 1024; # 支持的最大连接数
}

nginx服务器中配置最频繁的部分,
代理、缓存、日志绝大多数功能和第三方模块都在这,
http块包括:

  • http全局块
    • 包括文件引入、MIME-TYPE定义、日志自定义、连接超时时间、单链接请求数上限等
  • server块
    • 和虚拟主机有密切关系
    • 又包括全局server和location
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      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;
      listen 8080; # 端口号
      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;
      # }
      #}

04 nginx配置实例

4.1 反向代理

4.2 负载均衡

4.3 动静分离

05 nginx原理