Nginx系列篇

一、Nginx安装

1、安装相关依赖环境

yum install gcc;
yum install pcre-devel;
yum install zlib zlib-devel;
yum install openssl openssl-devel;

2、下载nginx安装包

wget http://nginx.org/download/nginx-1.12.2.tar.gz

3.解压nginx

tar -zxvf nginx-1.12.2.tar.gz

4.编译安装

cd nginx-1.12.2/ 进入nginx-1.12.2目录
./configure 

--prefix=/usr/local/nginx                        #nginx安装目录

--user=nginx  --group=nginx                  #指定nginx用户以及用户组,也可以不用设置去掉这行  

--with-http_stub_status_module            #添加安装需要的status插件

--with-http_ssl_module                          #添加安装需要的ssl插件,如果需要实现Https的话就加上这行。当然如果已安装的nginx需要引入ssl插件也是可以的。

5.常用命令(进入上一步nginx编译安装目录/usr/local/nginx/sbin)

#启动nginx

nginx  

#热重启nginx(修改后nginx.conf文件的时候常用到)

nginx -s reload 

nginx配置文件原文件

#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;
        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;
    #    }
    #}

}
View Code

#检查nginx配置文件,也可以通过此命令查看当前nginx加载哪个配置文件启动的

nginx -t 

#指定自定义目录配置文件,用于nginx启动加载

nginx -c /usr/local/nginx/conf/nginx.conf  

#强制停止nginx

nginx -s stop

#优雅停止服务nginx

nginx -s quit

#查看nginx版本信息

nginx -v (小写的v)

 #查看nginx加载的配置文件和编译安装时候的config

nginx -V (大写的v)

二、Nginx location规则

1、location语法模板

location  [ = | ~ | ~* | ^~ | @ ]  pattern { 

      ......

}

2、修饰符 [ = | ~ | ~* | ^~ | @ ] 说明

2.1 精准匹配 [=] uri节点必须和pattern完全一致才能匹配 

server {   

       server_name dsz.com;    

       location = /test {  

       }

}

举例:

http://dsz.com/test 匹配

http://dsz.com/TEST 可能会匹配,也可以不匹配,取决于操作系统的文件系统是否大小写敏感。

http://dsz.com/test ?param1&param2 匹配,忽略 querystring

http://dsz.com/test/  不匹配,带有结尾的/

http://dsz.com/test2  不匹配

2.2 正则匹配 [~] uri节点区分大小写正则匹配

server { 

         server_name dsz.com;   

         location  ~  ^/test$ {     

         }

}

^/test$这个正则表达式表示字符串必须以/开始,以$结束,中间必须是test

http://dsz.com/test 匹配(完全匹配)

http://dsz.com/TEST 不匹配,大小写敏感

http://dsz.com/test?param1&param2 匹配

http://dsz.com/test/ 不匹配,不能匹配正则表达式

http://dsz.com/test2 不匹配,不能匹配正则表达式

2.3 正则匹配 [~*] uri节点不区分大小写正则匹配

server { 

         server_name dsz.com;   

         location  ~*  ^/test$ {     

         }

http://dsz.com/test 匹配(完全匹配)

http://dsz.com/TEST 匹配(大小写不敏感)

http://dsz.com/test?param1&param2 匹配

http://dsz.com/test/ 不匹配,不能匹配正则表达式

http://dsz.com/test2 不匹配,不能匹配正则表达式

2.4 正则匹配 [^~] uri节点前缀匹配

如果该 location 是最佳的匹配,那么对于匹配这个 location 的字符串, 该修饰符不再进行正则表达式检测。

注意,这不是一个正则表达式匹配,它的目的是优先于正则表达式的匹配

3、当有多条location规则时优先级如下:location查找匹配优先级

精确匹配=

前缀匹配^~(立刻停止后续的正则搜索)

按文件中顺序的正则匹配~或~*

匹配不带任何修饰的前缀匹配。

4、常用的location用法实例(带后续丰富)

匹配图片规则

location ~ .(gif|jpg|jpeg|png|bmp|swf)$ {

      root C:/test; #默认的图片路径,只要后缀是以上的都会到这个路径下搜索

}

location ~ .*.(js|css|png|svg|ico|jpg)$ {
      valid_referers none blocked 192.168.11.160 www.dsz.com; #防盗链处理
      if ($invalid_referer) {
                     return 404;
      }
      root static-resource;
      expires 1d;
}

后端代理(https证书配置)

server {

listen 8665;
server_name localhost; #填写绑定证书的域名

ssl on;
ssl_certificate /home/hz_dev/Fudan_Web/local-nginx-cert/server.crt;
ssl_certificate_key /home/hz_dev/Fudan_Web/local-nginx-cert/server.key;

ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #按>照这个套件配置
ssl_prefer_server_ciphers on;

location / {
            proxy_pass http://192.168.165.128:8788/;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Port $server_port;
      }

}

 三、Nginx模块

进入nginx解压目录下:使用 cat auto/options | grep YES 查看已安装或者默认安装过的模块

1、ngx_http_access_module(默认自带模块)模块存在的功能意义。

location / {

      #deny 192.168.50.175; #限制ip不能访问

      #allow 192.168.50.175; #允许ip能访问

      #allow all;  deny all; #允许或者限制全部不能访问
        root html;
        index index.html index.htm;
}

 2、如何安装第三方模块(插件)。

2.1、如果你当前机器没有安装nginx那么在初始编译安装是的时候可以直接加上你需要安装的插件模块。

 ./configure 

--prefix=/usr/local/nginx   #安装目录

--with-http_stub_status_module #安装的插件

--with-http_random_index_module #安装的插件

 2.2、如果你当前机器已安装过nginx,但是目前没有你想要的插件。

第1步: nginx -V 查看初始config的参数

--prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module 原来初始安装的参数

 第2步:假设你在初始安装nginx的时候没有加上--with-http_ssl_module。

那么这个时候相当于原来的config = --prefix=/usr/local/nginx --with-http_stub_status_module.

那么此时进入原来被解压的目录中:执行tar -zxvf nginx-1.12.2.tar.gz的目录里面执行

#1 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module(新加的插件)

#2 make(只要执行make,无需执行make install否则会覆盖原有的nginx影响原来的功能)

#3 cp /usr/local/nginx/sbin/nginx  nginx.bak 首先备份以前的启动程序

#4  cp /home/dsz/nginx-study/nginx-1.12.2/objs/nginx  /usr/local/nginx/sbin/nginx  复制obj新生成的启动程序,覆盖到以前的nginx

#5 nginx -t 检测nginx是否有问题

  以上表示加装插件成功。

 3、http_stub_status_module 插件功能作用 nginx监控平台

location /status {
      stub_status;
}
访问http://localhost/status
Active connections:当前状态,活动状态的连接数
accepts:统计总值,已经接受的客户端请求的总数
handled:统计总值,已经处理完成的客户端请求的总数
requests:统计总值,客户端发来的总的请求数
Reading:当前状态,正在读取客户端请求报文首部的连接的连接数
Writing:当前状态,正在向客户端发送响应报文过程中的连接数
Waiting:当前状态,正在等待客户端发出请求的空闲连接数 

 4、http_random_index_module 页面岁间展示插件(如果没有安装这个插件可以参考当前汶上上面所说的第三方插件安装方法做一遍)

location /random {
      alias /usr/local/nginx/html;
      random_index on; #随机展示/usr/local/nginx/html目录下的某个页面
}

 

 

、Nginx应用实战

1、nginx反向代理配置(这里就不给大家做例子演示了做个知识点归纳,给大家点一下其实反向代理晚上很多例子)

server{

                listen 80;
                server_name localhost;
                location / {
                       proxy_pass http://192.168.11.154:8080;
                }
}
 
2、nginx负载均衡
(1)轮询算法(默认), 如果后端服务器宕机以后,会自动踢出
(2)ip_hash 根据请求的ip地址进行hash
(3)权重轮询
upstream tomcat { 
      server 192.168.11.161:8080 max_fails=2 fail_timeout=60s; #试错次数 错误超时
server 192.168.11.159:8080; } server { listen 80; server_name localhost; location / { proxy_pass http://tomcat; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout http_500 http_503; proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET,POST,DELETE'; add_header 'Aceess-Control-Allow-Header' 'Content-Type,*'; } location ~ .*.(js|css|png|svg|ico|jpg)$ { valid_referers none blocked 192.168.11.160 www.dsz.com; #防盗链处理 if ($invalid_referer) { return 404; } root /my/images; expires 1d; } }

 配置说明

proxy_next_upstream
语法:proxy_next_upstream [error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 |
http_404 | off ];
默认:proxy_next_upstream error timeout;
配置块:http、server、location
这个配置表示当向一台上有服务器转发请求出现错误的时候,继续换一台上后服务器来处理这个请求。
默认情况下,上游服务器一旦开始发送响应数据,Nginx反向代理服务器会立刻把应答包转发给客户端。因此,一
旦Nginx开始向客户端发送响应包,如果中途出现错误也不允许切换到下一个上有服务器继续处理的。这样做的目
的是保证客户端只收到来自同一个上游服务器的应答。
proxy_connect_timeout
语法: proxy_connect_timeout time;
默认: proxy_connect_timeout 60s;
范围: http, server, location
用于设置nginx与upstream server的连接超时时间,比如我们直接在location中设置proxy_connect_timeout
1ms, 1ms很短,如果无法在指定时间建立连接,就会报错。
proxy_send_timeout
向后端写数据的超时时间,两次写操作的时间间隔如果大于这个值,也就是过了指定时间后端还没有收到数据,连
接会被关闭
proxy_read_timeout
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout http_500 http_503; proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET,POST,DELETE'; add_header 'Aceess-Control-Allow-Header' 'Content-Type,*'; }location ~ .*.(js|css|png|svg|ico|jpg)$ { valid_referers none blocked 192.168.11.160 www.gupaoedu.com; if ($invalid_referer) { return 404; }root static-resource; expires 1d; } }
从后端读取数据的超时时间,两次读取操作的时间间隔如果大于这个值,那么nginx和后端的链接会被关闭,如果
一个请求的处理时间比较长,可以把这个值设置得大一些
proxy_upstream_fail_timeout
设置了某一个upstream后端失败了指定次数(max_fails)后,在fail_timeout时间内不再去请求它,默认为10秒
语法 server address [fail_timeout=30s]
upstream backend { #服务器集群名字
#server 192.168.218.129:8080 weight=1 max_fails=2 fail_timeout=600s;
#server 192.168.218.131:8080 weight=1 max_fails=2 fail_timeout=600s;
}

五、Nginx静态资源服务器

1、静态文件(jpg | js | css)等缓存压缩处理

  server_tokens off;#不显示nginx版本信息

  #以下zip相关参考博主:https://www.nginx.cn/doc/standard/httpgzip.html

  gzip on;#是否开启压缩
  gzip_min_length 1k;#大于当前值就会执行压缩
  gzip_buffers 4 16k;#gzip申请内存大小每次已16k的4被去申请
  #gzip_http_version 1.0;#http协议版本
  gzip_comp_level 2;#压缩级别。级别越高压缩效比例越大,但是越高会消耗更多的CPU资源
  gzip_types text/plain application/x-javascript application/css text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;#对哪些文件进行压缩
  gzip_vary on;#Accept-Encoding: gzip是否显示 参考博主:http://www.04007.cn/article/644.html
  gzip_disable "MSIE [1-6].";#正则匹配什么样的浏览器不进行gzip

  gzip_proxied;#无条件压缩所有结果,页面做后端代理的时候设置当前这个值 

location ~ .*.(js|css|png|svg|ico|jpg)$ {
#none表示当前不存在域也允许访问 例如:直接在浏览器里打开图片地址就没有域
#blocked表示存在的域允许被访问 #valid_referers none blocked 192.168.11.160 www.dsz.com; #防盗链处理 参考博主:https://blog.csdn.net/qq_36663951/article/details/80222626 #if ($invalid_referer) { #return 404; #} root /usr/local/nginx/html; expires 1h;#30s|m|h|d }

 六、Nginx跨域问题

server{
     listen 80;
     server_name localhost;
     location / {
                proxy_pass http://192.168.11.154:8080;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_send_timeout 60s;
                proxy_read_timeout 60s;
                proxy_connect_timeout 60s;
                add_header 'Access-Control-Allow-Origin' '*'; // #允许来自所有的访问地址
                add_header 'Access-Control-Allow-Methods' 'GET,PUT,POST,DELETE,OPTIONS'; //支持的 请求方式
                add_header 'Access-Control-Allow-Header' 'Content-Type,*'; //支持的媒体类型
      }
      location ~ .*.(gif|jpg|ico|png|css|svg|js)$ {
                root static;
      }
}

七、Nginx集成Lua实现代码编程(注意当前博主的nginx版本是1.12.2

大家在集成lua的时候要注意nginx和当前lua版本的问题,本人当时在安装的时候遇到过此类问题。这里给大家演示两种方式集成lua的方式。

类似:error: incompatible types when assigning to type 'struct ngx_buf_t *'

方式一:已存在历史安装的Nginx,不想重新安装Nginx只在原来基础上升级。

第一步:首先检查当前Nginx是否存在lua插件,执行:nginx -V (注意这里的V是大写的)。从下图分析博主当前是一安装过了的,没有安装件不会看到红色部分的关键字眼。

 第二步:下载三个lua插件相关安装文件,如图所示下面三个红色的文件。实践操作nginx-1.12.2匹配下面三个版本是可以兼容的再集成过程中。

如果版本不兼容或出现:error: incompatible types when assigning to type 'struct ngx_buf_t *'类似的错误。

创建一个自定义目录,开始下载三个文件

wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.0.tar.gz
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz

第三步:开始解压安装以上三个文件,注意其中只有LuaJIT-2.0.5.tar.gz需要安装,其他两个解压就好了。

解压安装:LuaJIT-2.0.5.tar.gz
$ tar -zxvf LuaJIT-2.0.5.tar.gz $ cd LuaJIT-2.0.5 $ make install PREFIX=/usr/local/your_path
最后出现Successfully installed LuaJIT 2.0.5 to /usr/local/your_path表示成功
配置环境变量
vi /etc/profile
======>>>>>>
export LUAJIT_LIB=/usr/local/your_path/lib
export LUAJIT_INC=/usr/local/your_path/include/luajit-2.0

<<<<<<======
source /etc/profile 是配置文件生效
解压:v0.3.0.tar.gz
tar -zxvf v0.3.0.tar.gz
解压:v0.10.13.tar.gz
tar -zxvf v0.10.13.tar.gz

 第四步:进入原来nginx解压的目录重新编译,如下目录,找不到可以重新下载解压一个ngonx-tar包就好了。

nginx -V查看原来configure参数

原来的:./configure  --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_random_index_module

当前需要改成:./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_random_index_module --with-ld-opt=-Wl,-rpath,/usr/local/your_path/lib --add-module=/home/dsz/nginx-study/lua-nginx/ngx_devel_kit-0.3.0 --add-module=/home/dsz/nginx-study/lua-nginx/lua-nginx-module-0.10.13

1、执行当前以上最新configure。

 --with-ld-opt=-Wl,-rpath,/usr/local/your_path/lib(这里是make install之后的路径下的lib

--add-module=/home/dsz/nginx-study/lua-nginx/ngx_devel_kit-0.3.0(解压之后的路径

--add-module=/home/dsz/nginx-study/lua-nginx/lua-nginx-module-0.10.13(解压后的路径

 这三个参数记得指定好自己本地的路径。

 2、make -j2 不需要再make install

3、复制重新编译后的nginx到原来nginx安装的/usr/local/sbin/ 目录。这样lua插件就已经集成成功了。

cp /home/dsz/nginx-study/nginx-1.12.2/objs/nginx  /usr/local/sbin/ 

4、演示一下

在nginx的配置文件nginx.conf的server节点配置lua节点

location /hello_lua {
               default_type 'text/plain';
               content_by_lua 'ngx.say("hello, lua!")';
}

重启nginx:nginx -s reload。出现以下提示可以忽略不管,不影响nginx功能。

 访问:localhost/hello_lua出现以下响应,表示成功。

 方式二:使用Openresty(升级版的Nginx,类似于Spring和SpringBoot)。

1、下载:wget https://openresty.org/download/openresty-1.19.3.1.tar.gz (下载地址:https://openresty.org/cn/download.html)

2、tar -zxvf openresty-1.19.3.1.tar.gz

3、cd openresty-1.19.3.1

4、./configure 默认会安装到/usr/local/openresty中

5、make

6、make install

这样Openresty就安装好了。

 演示:

同样的找到Openresty方式安装的nginx目录,修改nginx.conf

cd /data/program/openresty/nginx/conf
location /openresty_test {
      default_type text/html;
      content_by_lua_block {
              ngx.say("helloworld");
      }
}

 访问http://localhost/openresty_test,也会出现helloworld表示成功。

总结:

Openresty的好处就是安装好的nginx自带lua插件,无需像原来方式单独引入lua插件。

 八、Nginx Lua高级篇

 参考极客教程:https://wiki.jikexueyuan.com/project/nginx-lua/client.html

 九、Nginx实现灰度发布

worker_processes 1; 
error_log logs/error.log; 
events{ 
 worker_connections 1024; 
} 
http{ 
 lua_package_path "$prefix/lualib/?.lua;;"; 
 lua_package_cpath "$prefix/lualib/?.so;;"; 
 upstream prod { 
 server 192.168.11.156:8080; 
 } 
 upstream pre { 
 server 192.168.11.156:8081; 
 } 
 server { 
 listen 80; 
 server_name localhost; 
 location /api { 
 content_by_lua_file lua/gray.lua; 
 } 
 location @prod { 
 proxy_pass http://prod; 
 } 
 location @pre { 
 proxy_pass http://pre; 
 } 
 } 
 server { 
 listen 8080; 
 location / { 
 content_by_lua_block { 
 ngx.say("I'm prod env"); 
 } 
 } 
 } 
 server { 
 listen 8081; 
 location / { 
 content_by_lua_block { 
 ngx.say("I'm pre env"); 
 } 
 } 
 } 
}
编写 gray.lua 文件
local redis=require "resty.redis"; 
local red=redis:new(); 
red:set_timeout(1000); 
local ok,err=red:connect("192.168.11.156",6379); 
if not ok then 
 ngx.say("failed to connect redis",err); 
 return; 
end 
local_ip=ngx.var.remote_addr; 
local ip_lists=red:get("gray"); 
if string.find(ip_lists,local_ip) == nil then 
 ngx.exec("@prod"); 
else 
 ngx.exec("@pre"); 
end 
local ok,err=red:close();

十、Nginx Https自签发证书

参考两位博主:

https://www.cnblogs.com/fengyuduke/p/11232662.html

https://www.cnblogs.com/deepalley/p/12875412.html

十一、Nginx配置文件参考

#user  nobody;
worker_processes  2;#cpu核心数

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

#pid        logs/nginx.pid;


events {
    use epoll; #IO模型 select poll epoll等
    worker_connections  1024;#单个worker进程的连接数,上线为:ulimit -n
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    
    #关闭代码缓存 。修改lua脚本不需要重启
    lua_code_cache off;

    #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;
    
    server_tokens off;#不显示nginx版本信息
    
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    #gzip_http_version 1.0;
    gzip_comp_level 2;#压缩级别
    gzip_types text/plain application/x-javascript application/css  text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary on;#Content-Encoding: gzip是否显示
    gzip_disable "MSIE [1-6].";  
    gzip_proxied any; 

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        
        location /test {
            default_type application/json;
            return 200 'my nginx test';	
        }


        location /allow {
            allow 192.168.50.175; #允许ip能访问 
            alias  /usr/local/nginx/html;
            index  index.html index.htm;
        }
       
        location /deny {
            deny 192.168.50.175; #限制ip不能访问
            alias  /usr/local/nginx/html;
            index  index.html index.htm;
        }
       

        location /random {
            alias  /usr/local/nginx/html; 
            random_index on; #随机展示root->html目录下的某个页面
#            index  index.html index.htm;
        }



        location /status {
            stub_status; #nginx监控 
        }

        location ~ .*.(js|css|png|svg|ico|jpg)$ { 
                  default_type application/json;
	          valid_referers  192.168.1.5; #防盗链处理
	          if ($invalid_referer) {
      		       # return 500;
                       # default_type application/json;
                        return 200 '文明上网,不要盗图!'; 
	          }
		  root /usr/local/nginx/html; 
	          expires 1h; 
	}

        location /hello_lua { 
               default_type 'text/plain'; 
               content_by_lua 'ngx.say("hello, lua!")'; 
        }

        #设置变量
        set $name "shouzhi@duan";
        location /lua_test {
                content_by_lua '
                        ngx.header.content_type="text/plain";
                        ngx.say(ngx.var.name);
                ';
        }
 

        #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;
    #    }
    #}

}

十二、设置代理缓冲区

proxy_buffer_size 64k;
proxy_buffers 32 64k;
proxy_busy_buffers_size 128k;

参考:https://blog.csdn.net/yyj108317/article/details/109484923

 总结:文章思路排版做的不是很好,但是常用Nginx知识点都涉及。就当是提供学者的一个思路笔记,里面每个环节深究都很有学问,希望能帮助到大家。



原文地址:https://www.cnblogs.com/dszazhy/p/14776604.html