nginx简单应用

85 阅读4分钟

一、nginx的安装

1.1前置环境

linxu需要有gcc环境,入股没有可通过 yum -y install gcc-c++ 安装; nginx默认开启了gzip,rewrite,ssl功能,因此依赖zlib,pcre,openssl库,如果没有可通过 yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel 安装 1.2 nginx的安装 tar -xvf nginx的安装包 进入到nginx的安装目录,执行 ./configure命令编译nginx源码,该命令执行成功后,执行 make && make install 编译并安装nginx。此时nginx会安装到 /usr/local/nginx目录下。在执行./configure命令时,如果指定参数 --prefix=path,可以指定nginx的安装目录,即 ./configure --prefix=path。 1.3 nginx的启动,停止 启动:进入到 nginx的sbin目录执行 ./nginx命令即可启动。 停止: ps -ef |grep nginx 查看nginx的pid ,然后执行 kill -9 pid即可停止nginx。

二、nginx的常用功能

2.1 反向代理

反向代理:对外提供代理服务器的地址,而隐藏真实提供服务的ip地址。

image.png

2.1.1 ngixn实现反向代理主要使用的proxy_pass指令。

server {
                  listen 80;
                  server_name localhost;
                  location / {
                          root html;
                         proxy_pass http://ip:8090;
                          index index.html index.htm;
                          }
          }

           server {
                    listen 80;
                    server_name localhost;
                    location ~ /edu/ {
                            root html;
                            proxy_pass http://ip:8090;
                            index index.html index.htm;
                           }
                  location ~ /dev/ {
                          proxy_pass http://ip:8091;
                  }
           }

2.3 负载均衡

根据一定的策略将多个请求分发到不同的服务器,以减轻一台服务器的压力。

image.png

2.3.1 nginx实现负载均衡主要依靠 upstream和proxy_pass指令。

nginx默认实现负载均衡的方式为轮询,可通过weight参数指定其权重。其配置如下:

               server ip:8090 weight=1;
               server ip:8091 weight=1 backup(备份服务器,当主服务器不可用时,会启用该服务);
             }
 
         server {
                  listen 80;
                  server_name localhost;
                  location /{
                         root html;
                          proxy_pass http://backedserver;
                          index index.html index.htm;
                          }
          }
          注意:tomcat8及以上版本,其upstream后面的名称不要带_,否则会报错

2.4 nginx实现动、静分离

                  listen 80;
                  server_name localhost;
                 location /{
                         root html;
                          proxy_pass http://backedserver;
                          index index.html index.htm;
                          }
                  #动,静分离
                  location ~ .*\.(html|htm|js|css|png|jpg|ico|txt|gif) { 
                        root /software/static;
                 }
         }

2.5常用指令介绍

2.5.1 proxy_pass指令的使用:

1.proxy_pass指令中的url是否包含uri:如果包含uri,则会使用新的uri替代旧的uri(匹配上的部分);如果不包含uri则不会改变原来地址的uri

server {
                  listen 80;
                  server_name localhost;
                  location /test/ {
                          root html;
                         proxy_pass http://ip:8091/dev/a.html;
                          index index.html index.htm;
                          }
          }

此时如果请求的url为http://localhost/test,则经过nginx后会转向为 http://ip:8090/dev/a.html
server {
                  listen 80;
                  server_name localhost;
                  location /dev/ {
                          root html;
                         proxy_pass http://ip:8091;
                          index index.html index.htm;
                          }
          }

此时如果请求的url为http://localhost/dev/a.html,则经过nginx后仍会转向为 http://ip/dev/a.html

2.proxy_pass指令后的url变量的末尾是否添加"/"

proxy_pass http://ip:8091;
proxy_pass http://ip:8091/;

此时后者与前者的区别是后者包含了URI"/",前者没有包含URI;

server {
                  listen 80;
                  server_name localhost;
                  location /dev/ {
                          root html;
                         proxy_pass http://ip:8091;
                         proxy_pass http://ip:8091/;
                          index index.html index.htm;
                          }
          }
此时如果请求的地址为 http://localhst/dev/a.html,前者的请求地址为 http://ip:8091/dev/a.html;后者的请求地址则为http://ip:8091/a.html

proxy_pass代理地址端口后无任何字符,转发后地址:代理地址+访问URL目录部分
proxy_pass代理地址端口后有目录(包括 / ),转发后地址:代理地址+访问URL目录部分去除location匹配目录(示例中的"dev"或"dev/")

2.5.2 location 指令

location是用来匹配不同的url请求,进而做出不同的处理和响应。 location 的匹配规则

  1. 匹配url,有四种参数可选:[= | ~ | ^~ | ~* ] uri {}

“=”表示精准匹配 “^~” 普通字符串匹配上以后不再进行正则匹配 “ ~* ”执行正则匹配,不区分大小写 “~执行正则匹配,区分大小写 不加任何规则时默认大小写敏感,前缀匹配,相当于加了 ^~和 ~。如: /index/ {}

2.5.3 root命令和alias命令

root指令:指定请求文档的根目录,其路径最后"/"可带可不带

location /img/{
                root /img/static;
           }
 当请求路径为 localhost/img/1.jpg时其真正路径为 /img/static/img/1.jpg

alias指令:与root指令类似,目录别名的定义,其路径最后必须带有"/",在带有正则表达式的location中不能使用该指令

location /img/{
                alias /img/static/;
           }
 当请求路径为 localhost/img/1.jpg时其真正路径为 /img/static/1.jpg

2.5.4 index指令

index指令:用于指定默认首页文件

location /img/{
                index index.htm index html;
                autoindex on; #如果默认首页没找到会显示当前目录的文件列表
           }

2.6 其他应用

2.6.1 搭建nginx为文件服务器并实现文件的下载

配置如下:

location /doc{
                         autoindex on; #开启目录列表
                         charset utf-8,gbk; #解决中文乱码
                         sendfile on; #开启高效传输文件
                         alias /opt/sql/;
                         #实现文件下载
                         if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){
                                    add_header Content-Disposition: 'attachment;';
                                    add_header Content-Type application/ocet-stream;
                          }
               }


2.6.2 将日志格式该文json格式

 log_format nginx_json escape=json '{'
                      '"remote_addr":"$remote_addr",'
                      '"remote_user":"$remote_user",'
                      '"uri":"$request",'
                      '"http_referer":"$http_referer",'
                      '"http_user_agent":"$http_user_agent",'
                      '"forwarded":"$http_x_forwarded_for"'
                      '"status":"$status",'
                      '"request_time":"$time_local"'
                      '}';

    #access_log  logs/access.log  main;
	
access_log  logs/access.log nginx_json;