nginx

本文详细介绍了Nginx的常用命令,如reload、start/stop/squit,以及location和rewrite的区别。重点讲解了负载均衡、反向代理配置,包括ip_hash和HTTPS重定向,还有stream模块的应用。

nginx命令

nginx -s reload #重启nginx
nginx -v
nginx -V #包括查看安装的模块
nginx -s stop #立即停止nginx
nginx -s quit #等待到请求被处理完成后停止nginx
nginx #启动nginx

which nginx #查看可执行文件位置

nginx -t #测试配置并显示配置文件路径

指令块

http块:http块中可以嵌套多个server块。

location块:location块放在server块中,location配置请求的路由,以及各种页面的处理情况。

proxy_pass指令:proxy_pass起到反向代理的作用,proxy_pass放在location{}中。

rewrite 与 location

rewrite和location都能实现跳转。

  • rewrite是在同一域名内更改获取资源的路径,实现url重写和重定向,rewrite可放在server{},location{},if{}中。
  • location是对一类路径做控制访问或反向代理。

负载均衡

stream模块用来实现四层协议的转发、代理或者负载均衡等。

upstream模块定义集群名称和节点地址,定义在server字段之外http字段之内。nginx将请求转发给这个集群中的服务,实现负载均衡等功能。

stream {
    upstream backend1 {
        ip_hash; #源地址hash调度方法
        hash $remote_addr consistent; #一致性hash
        server 192.168.182.155:3306 weight=5 max_fails=3 fail_timeout=30s;
        server 127.0.0.1:8080 weight=2;
        server 127.0.0.1:8081;
    }
    server {
        listen 80;#数据库服务器监听端口
        proxy_connect_timeout 10s;
        proxy_timeout 300s;#设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。
        proxy_pass backend1;
    }
}

stream和http是同级别的,不要放入http里面。

如果重启nginx报错:unknown directive "stream",则可通过动态加载stream模块解决:

  • 下载相同版本nginx,http://nginx.org/download/nginx-1.10.1.tar.gz,解压。
  • 通过 ./nginx -V 查看configure arguments。
  • 配置 ./configure 原参数,在最后加上--with-stream --with-stream=dynamic
  • make
  • 拷贝objs/ngx_stream_module.so 到 /usr/local/nginx/modules/ngx_stream_module.so
  • 在nginx.conf中event行前加入  load_module /usr/local/nginx/modules/ngx_stream_module.so; 重启nginx。

nginx反向代理默认情况下会轮询集群中的服务。如果加了ip_hash标志,则固定客户端总是会被反向代理到固定的一个服务上。即同一客户端ip会被分配给固定的后端服务器,这样可以解决session问题。 

代理配置

前端服务中配置反向代理,请求被传递到被代理地址。配置文件位于:/usr/local/nginx/conf/vhost。

server{
    listen 80;
    listen 443 ssl;

        #ssl_certificate /usr/local/nginx/conf/wwkssl.crt;
        #ssl_certificate_key /usr/local/nginx/conf/wwkssl.key;
        ssl_certificate /etc/letsencrypt/live/www.xxx.cn/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/www.xxx.cn/privkey.pem;

        server_name xxx.cn www.xxx.cn;

 #nginx 对文档检测比较严格,所以if  ( $host != 'www.csdn.com'  ) 这些代码之间需要有空格隔开,不然会报错:unknown directive “if($host!=”
 if ( $host != 'www.xxx.cn' )
 {
   rewrite ^/(.*)$http://www.xxx.cn/$1 permanent;
 }

        #proxy_set_header X-Forwarded-Host $host;
        #proxy_set_header X-Forwarded-Server $host;
        #proxy_set_header X-Real-IP $remote_addr;
        #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #proxy_set_header Host $host:$server_port;

        location / {
                proxy_pass https://被代理地址;

                proxy_set_header Host $host;
                proxy_set_header X-Real-IP       $remote_addr;
                proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

        if ($server_port = 80) {
                return 497;
        }

        #让http请求重定向到https请求
        #https://$host$uri?$args;
        error_page 497 https://$host$uri;
 }

上面代码块中rewrite的作用是使不带www的域名重定向到带www的域名,其中$host变量的值等于请求头中Host的值。当Host无效时就是处理该请求的server的名称。

permanent表示永久性重定向,请求日志中的状态码为301。

nginx 对文档检测比较严格,所以if ( $host != 'www.csdn.com' ) 这些代码之间需要有空格隔开,不然会报错:unknown directive “if($host!=”。

在被代理服务器中配置如下:

server {
        listen 80;
        listen 443 ssl;
       

        #当同端口多域名时,server_name后要配置为具体域名
        server_name localhost;
        index index.html index.htm index.php;
        root /xxx/www/xxx;

        ssl_certificate /xxx/server/nginx/conf/wwkssl.crt;
        ssl_certificate_key /xxx/server/nginx/conf/wwkssl.key;

        location ~ .*\.(php|php5)?${
                #fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${
                expires 30d;
        }
        location ~ .*\.(js|css)?${
                expires 1h;
        }
       
        include /xxx/server/nginx/conf/rewrite/wordpress.conf;
        access_log  /xxx/log/nginx/access/xxx.log;
}

#反向代理到java web 8090端口服务
location /products {
    proxy_pass http://localhost:8090/products;
}

    配置静态图片地址

    #静态图片目录

    location /details/images {

            alias D:/Pictures/dev_test;  # 设置外部目录的路径

            autoindex on;      # 可选:开启目录浏览

    }

    例如请求 http://127.0.0.1/details/images/abc/2.jpg,可访问到D:/Pictures/dev_test/abc/2.jpg。

    注意:

    • location 中最后的反斜杠要与alias中最后的反斜杠一致,都有或者都没有。
    • 在linux中不要把目录配置到/root目录下,非所有者同组的用户没有读权限。

    重写并重新匹配

    #重写
    location ~ ^/details/images/(.+)/cover$ {
        # $1 是之前捕获的路径部分,last 表示使用新的 URI 重新匹配location块。
        rewrite ^/details/images/(.+)/cover$ /details/images/$1/compressO-0.jpg last; 
    }

    location匹配模式修饰符

    location匹配模式修饰符用在location和路径之间,支持以下修饰符:

    修饰符匹配规则优先级典型场景
    =精确匹配最高健康检查接口
    ^~前缀匹配次高静态资源目录
    ~正则匹配(区分大小写)中等API版本路由
    ~*正则匹配(不区分大小写)中等国际化URL
    无修饰符前缀匹配最低默认回退路径

    Request Entity Too Large

    原因是nginx设置了允许上传的包的问题。

    打开nginx主配置文件nginx.conf,找到http{}段并修改以下内容:

    #413 Request Entity Too Large
    # 这将仅对该服务器块中的请求生效。
    client_max_body_size 100M;

    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值