美文网首页
Nginx -- Web服务器

Nginx -- Web服务器

作者: saoraozhe3hao | 来源:发表于2018-10-29 16:15 被阅读0次

历史:2004年俄罗斯人 伊戈尔·赛索耶夫 发布Nginx
官网:http://nginx.org/
第三方模块:https://www.nginx.com/resources/wiki/modules/index.html

概念
正向代理:客户端访问服务器地址,本地的代理模块会把请求改造成访问代理的地址,通过代理访问实际的服务器
反向代理:客户端访问代理的地址,通过代理访问实际的服务器
Nagle算法:将要发送的数据存放在缓存里,当积累到一定量或一定时间,再将它们发送出去
Lua:一种轻量小巧的脚本语言
URI:Uniform Resource Identifier,统一资源标识符,标识一个资源,例如 cay@horstman.com
URL:Uniform Resource Locator,统一资源定位符,定位一个资源,例如http://www.test.com/index.html,是URI的子集

Nginx作为LB 与 HAProxy对比
1、Nginx除了LB 还有 Web 服务器功能
2、HAProxy是单进程,Nginx是多进程,可以利用多核
3、Nginx社区更活跃

Nginx作为Web服务器 与 Apache HTTP Server 对比
1、Nginx轻量级,占用资源少
2、Apache是同步多进程模型,一个连接对应一个进程;Nginx是异步多进程模式,多个连接对应一个进程,支持更多的并发连接
3、Nginx处理静态文件效率高
4、Nginx 配置简洁

CentOs安装:yum install epel-release; yum install nginx
Ubuntu安装:apt-get update; apt-get install nginx
tar.gz安装

tar -zxvf nginx.tar.gz
./configure --without-http_rewrite_module --without-http_gzip_module  # 配置,不联网时忽略rewrite 和 gzip 模块
make
make install   # 会提示所安装的目录
./sbin/nginx  # 在nginx 安装目录里 执行启动

启停:
service nginx start|restart|stop
nginx -s reload|reopen|stop|quit
配置文件检查:nginx -t

配置

/etc/conf/nginx.conf

user nginx;                       # worker进程所属的用户组
worker_processes auto;  # worker进程数量
error_log /var/log/nginx/error.log;  # 错误日志
pid /run/nginx.pid;            # 记录主进程ID的文件

include /usr/share/nginx/modules/*.conf;  # 引入其他配置文件

events {
    worker_connections 1024;    # 一个worker的最大连接数
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    # $upstream_addr 上游地址
    access_log  /var/log/nginx/access.log  main;   # 访问日志 路径 和 格式
    # access_log off; # 关闭访问日志

    sendfile            on;                    # 把文件数据从磁盘直接传到套接字,不经过buffer
    tcp_nopush          on;               # 启用套接字TCP_CORK选项,即sendfile时尽可能一次发完
    tcp_nodelay         on;               # 禁用Nagle算法
    keepalive_timeout   65;           # 在响应头中设置keepalive
    types_hash_max_size 2048;
    client_max_body_size   20m;  # 最大上传量,也可以放在server{} 或  location{} 里

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {     # 一个虚拟服务器
        listen       80 default_server;   # default_server表示对80端口的请求,任何 server_name都不匹配时,选用这个server
        listen       [::]:80 default_server;  # 监听IPv6的80端口
        server_name  www.test.com;     # 这个server绑定的域名
        root         /usr/share/nginx/html;  # 静态文件根目录,不设置root,则默认为 /usr/share/nginx/html

        include /etc/nginx/default.d/*.conf;

       if ($scheme = http) {
            return 301 https://$server_name$request_uri;  // 将http重定向到https,要写在监听80端口的server{}下
        }
        location ^~ /static/ {  
            etag on;  # 是否开启etag机制,默认为on
            if_modified_since exact;  # 是否响应if_modified_since,默认为exact 
            expires [time|epoch|max|off] # 是否开启expires机制
            root /webroot/static/;     # 访问本地文件, 该目录下如果没有index文件,会出现403
        }  
    location /contract {
                // alias 和 root 二选一
               alias /usr/;  # 实际访问的是 /usr/
               root /usr/;   # 实际访问的是 /usr/contract 
               add_header Access-Control-Allow-Origin *;  # 允许跨域访问
        }
        location / {
           proxy_pass http://tomcat:8080/   # 转发给后端服务器,如果这里使用了变量,则需要配置 resolver 来指定DNS
           proxy_read_timeout  3600;   # 超时时间
        }

        error_page 403 404 /404.html;  # 碰到403、404 则 跳转到 /404.html
        location = /404.html {
            root /usr/static;
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    server{
        # 第二个server
    }
}

location匹配规则
精确匹配:=,例 location = /login
匹配路径:^~,例 location ^~ /static/ 匹配 /static/a.html
区分大小写的正则匹配:~,location ~ .(gif|jpg|png|js|css)$
不区分大小写的正则匹配:~*
区分大小写的正则不匹配:!~
不区分大小写的正则不匹配:!~*
通配:/

匹配优先级:=、^~、正则匹配(按书写顺序)、/

全局变量

$args: 请求行中问号后面的参数
$content_length: 请求头中的Content-length字段。
$content_type: 请求头中的Content-Type字段。
$host: 请求目标主机
$http_user_agent: 客户端agent信息
$http_cookie: 客户端cookie信息
$request_method: 客户端请求的方法
$remote_addr: 客户端的IP地址。
$remote_port: 客户端的端口。
$request_filename: 当前所请求文件的路径
$scheme: HTTP协议(如http,https)
$server_protocol: 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$server_port: 请求到达服务器的端口号。
$request_uri: 请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
$uri: 不带参数的当前URI,不包含主机名,如”/foo/bar.html”。
$document_uri: 与$uri相同

反向代理

location ^~ /uri {
    proxy_pass http://tomcat:8080/newuri;   # 转发时可以改变端口 和 路径,如果location是正则匹配,转发时路径不会变

    # 设置给上游服务器的 头
    proxy_set_header Host $host;                            # 客户端请求的目标host,即Nginx
    proxy_set_header Proxy-Host $proxy_port;       # Nginx请求的目标host,即上游服务器
    proxy_set_header X-Real-IP $remote_addr;      # 客户端host
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 请求头中的X-Forwarded-For + $remote_addr
    # client -> nginx -> nginx 结构中,第一台nginx的$remote_addr是客户端,$proxy_add_x_forwarded_for也为$remote_addr,第二台nginx的$remote_addr为第一台nginx,$proxy_add_x_forwarded_for为第一台nginx的$proxy_add_x_forwarded_for + 第一台nginx,即客户端 + 第一台nginx
    sub_filter 'a' 'b';  # 响应内容替换
    sub_filter_once off;  # 非单处替换
}
upstream tomcats{   # 定义上游服务器
    server 10.0.100.10:11211;
    server 10.0.100.20:11211;
    keepalive 64;
}
location ^~ /uri {
    proxy_pass http://tomcat;   # 使用upstream 
}

负载均衡算法
1、轮询(默认算法)
2、IP hash
3、最少连接数

upstream tomcats{   # 定义上游服务器
    server 10.0.100.10:11211;
    server 10.0.100.20:11211;
    least_conn;   # 开启最少连接数 算法
}

HTTPS

server {
    listen 443 default ssl;
    server_name www.example.com example.com;

    ssl_prefer_ server_ ciphers on;
    ssl_protocols TLSvl SSLv3;
    ssl_ciphers RC4:HIGH:!aNULL:!MD5:@STRENGTH;
    ssl_session_cache shared:WEB:10m;

    ssl_certificate /usr/local/etc/nginx/www.example.com.crt;  # 证书
    ssl_ certificate_ key /usr/local/etc/nginx/www.example.com.key;  # 私钥

    location / {
        # https页面 访问的资源也必须是 https
        add_header Content-Security-Policy upgrade-insecure-requests;  # 如果html引用了http资源,强制改为https
        proxy_set_header X-FORWARDED-PROTO https;  # 让上游服务器知道原始请求是https
        proxy_pass http://upstream;  # nginx和上游之间不用https
        # proxy_pass https://upstream;  # nginx和上游之间也用https
    }
}

gzip 压缩

http {
    gzip on;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_disable msie6;

    gzip_types text/plain text/css application/x-javascript text/xml
        application/xml application/xml+rss text/javascript
        application/javascript application/json;
}

rewrite

执行顺序
1、执行server块的rewrite
2、匹配location
3、执行location块的rewrite,如果URL被重写,则重新执行1-3,直到匹配location 并 不被 rewrite
4、如果循环超过10次,则返回500 Internal Server Error错误

格式

rewrite 正则 新串 [标志位];
例:rewrite ^/images/(.*)_(\d+)x(\d+)\.(png|jpg|gif)$ /resizer/$1.$4?width=$2&height=$3? last;
含义:原url中的路径部分 去匹配 正则,把匹配串替换为新串,新串中的$1即引用匹配串中的第1个分组(小括号)

标志位flag
last: 后续只匹配,不再rewrite
break: 用在location中,不再rewrite,采用当前location
redirect: 返回302临时重定向,地址栏会显示跳转后的地址
permanent: 返回301永久重定向,地址栏会显示跳转后的地址

server {
    if ($request_method = POST) {    # if里的判断运算符 与 location 判断运算符 含义一致
        return 405;     # 直接返回状态码
    }
    location / {
        rewrite ^/listings/(.*)$ /$remote_port.html?listing=$1 last;  # 新串中可以引用全局变量$remote_port等
          if ( $http_user_agent ~* "(Android|iPhone|Windows Phone|UC|Kindle|Mobile)" ){    # 移动端
               rewrite  ^/(.*)$ /phone/$request_uri;   # 移动端重定向到 /phone路径
           } 
        proxy_pass http://upstream;
    }
}

try_file

location ^~ /test/ {
    index index.html;
    # try_files 表示找到一个可用的文件返回
    try_files /2.html /1.html /test/test2.html @bd;  # 引用location
}
        
location @bd {      # 定义location
    rewrite ^/(.*)$ http://www.google.com;
}

案例

1、小程序里需要打开第三方网页,用我方域名代理第三方域名

location ~ "^\/.*\..*\/" {    # 识别出 "https://我方域名/某域名/path" 格式的请求;需要转义的正则表达式得用引号包起来
       proxy_pass https:/$request_uri;  # 拼接出 "https://某域名/path"
       proxy_set_header Referer 'www.目标.com';
}

location / {
  proxy_pass https://www.目标.com;
  proxy_set_header Referer 'www.目标.com';
        
  proxy_set_header Accept-Encoding "";  # 避免gzip,使得sub_filter能正常工作
  sub_filter_once off;   # 替换所有
  sub_filter https:// https://www.我方.cn/;   # 从目标获取的响应内容里,在所有写明的域名前加一个我方域名,形成https://我方域名/某域名/path;避免页面直接向别的域名发送请求,造成跨域限制(CORS、防盗链)
    }

相关文章

  • Nginx多台服务器实现负载均衡

    Nginx负载均衡服务器: IP:192.168.0.4(Nginx-Server)Web服务器列表:Web1: ...

  • Nginx 入门(一)Nginx 配置Web服务器

    Nginx 入门(一)Nginx 配置Web服务器 引言 Nginx作为高性能的web和反向代理服务器,在互联网公...

  • Web框架与Web服务器

    1. Web框架与服务器区别 ​ web服务器:典型Web服务器Apache、Nginx、Tomcat,作用是接...

  • Nginx 架构初探

    引言:众所周知,Nginx 服务器是一个高性能的 Web 和反向代理服务器。Nginx 在激烈的 Web 服务器竞...

  • nginx简介

    nginx是什么 简单说,Nginx 是一个高性能的 Web服务器 和反向代理服务器。 web服务器 能够提供ht...

  • Nginx与Apache

    轻松使用Nginx搭建web服务器

  • Nginx-1 yum安装

    Web服务器介绍nginx Nginx基本使用** 获取Nginx 关闭防火墙关闭selinux Nginx安装:...

  • Nginx面试题

    什么是Nginx? nginx是一个web服务器,反向代理服务器,缓存服务器 为什么使用nginx 跨平台,配置简...

  • 2.4Nginx

    一.Nginx简介 什么是Nginx? Nginx是一款轻量级Web服务器,也是一款反向代理服务器. Nginx能...

  • Flask+uWSGI+Nginx模型

    Web服务器层 Web服务器主要是接收 HTTP 请求并返回响应。常见的 web服务器有 Nginx,Apache...

网友评论

      本文标题:Nginx -- Web服务器

      本文链接:https://www.haomeiwen.com/subject/xonbtqtx.html