标签:nginx

0

4494

systemd常用服务开机启动配置

由于ubuntu18.04开始,系统的开机启动配置不再使用/etr/rc.local,改用systemd方式.

现总结lnmp中常见的服务配置如下:

nginx

vim /lib/systemd/system/nginx.service

编辑nginx.service文件如下

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/data/service/tengine/logs/nginx.pid
ExecStartPre=/data/service/tengine/sbin/nginx -t
ExecStart=/data/service/tengine/sbin/nginx
ExecReload=/data/service/tengine/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

授权/重启服务载入配置/测试/加入开机启动,如下:

……

乐果   发表于   2019 年 04 月 13 日 标签:PHPnginxcaddy 继续阅读

0

3569

nginx配置中rewrite的坑

一、rewrite基本知识

在server块下,会优先执行rewrite部分,然后才会去匹配location块 server中的rewrite break和last没什么区别,都会去匹配location,所以没必要用last再发起新的请求,可以留空 location中的rewirte:

不写last和break - 那么流程就是依次执行这些rewrite

  1. rewrite break - url重写后,直接使用当前资源,不再执行location里余下的语句,完成本次请求,地址栏url不变
  2. rewrite last - url重写后,马上发起一个新的请求,再次进入server块,重试location匹配,超过10次匹配不到报500错误,地址栏url不变
  3. rewrite redirect – 返回302临时重定向,地址栏显示重定向后的url,爬虫不会更新url(因为是临时)
  4. rewrite permanent – 返回301永久重定向, 地址栏显示重定向后的url,爬虫更新url 使用last会对server标签重新发起请求

如果location中rewrite后是对静态资源的请求,不需要再进行其他匹配,一般要使用break或不写,直接使用当前location中的数据源,完成本次请求 如果location中rewrite后,还需要进行其他处理,如动态fastcgi请求(.php,.jsp)等,要用last继续发起新的请求 (根的location使用last比较好, 因为如果有.php等fastcgi请求还要继续处理) 使用alias指定源:必须使用last

……

乐果   发表于   2017 年 10 月 30 日 标签:nginx 继续阅读

0

4537

nginx+lua动态修改upstream实现动态代理

家里有台迷你主机作为家庭服务器,但是在公司或者其他地方想实时访问,以前常有的解决办法是用域名动态dns解析即 “花生壳” 来实现的,但问题是当家里的宽带ip发生变化时,“花生壳”虽然已经更新了dns的解析,但由于下游dns服务缓存的影响,生效会延迟,这样就存在临界时间段无法访问的问题。

最近,学习nginx+lua实现动态代理,即在外网服务器上搭一个代理,当家里ip发生改变时通知外网的代理服务;用户访问外网代理服务,间接访问家庭服务器时,已经知道ip发生变化了,这样即实现了无缝隙间断的访问~

nginx配置案例如下:

lua_shared_dict _ups_zone 64m;

server {
        listen       80;
        server_name  test.cn;
        access_log  off;

        location = /ups_update {
                default_type text/plain;
                content_by_lua '
                        local ups = ngx.req.get_uri_args()["ups"]
                        if ups == nil then
                                ngx.say("usage: /ups_update?ups=x.x.x.x")
                                return
                        end
                        local host = ngx.var.http_host
                        local ups_from = ngx.shared._ups_zone:get(host);
                        --ngx.log(ngx.WARN, host, " update upstream from ", ups_from, " to ", ups)
                        ngx.shared._ups_zone:set(host, ups);
                        ngx.say(host, " update upstream from ", ups_from, " to ", ups)
                ';
        }

        location / {
                set_by_lua $cur_ups '
                        local ups = ngx.shared._ups_zone:get(ngx.var.http_host)
                        if ups ~= nil then
                                --ngx.log(ngx.ERR, "get [", ups, "] from ngx.shared._ups_zone")
                                return ups
                        end
                '
                proxy_next_upstream off;
                proxy_set_header    Host $host;
                proxy_http_version  1.1;
                proxy_set_header    Connection  "";
                proxy_pass $scheme://$cur_ups;
        }
}

乐果   发表于   2017 年 09 月 30 日 标签:nginxlua 继续阅读

0

2425

lua_nginx_module常见使用文档

安装lua_nginx_module 模块

lua_nginx_module 可以一步步的安装,也可以直接用淘宝的OpenResty

Centos和debian的安装就简单了。。

这里说下freebsd的安装:

fetch http://www.lua.org/ftp/lua-5.1.4.tar.gz
tar zxvf lua-5.1.4.tar.gz
cd lua-5.1.4
make freebsd
make install
cd ..
fetch https://github.com/chaoslawful/lua-nginx-module/zipball/v0.1.6rc2
fetch https://github.com/simpl/ngx_devel_kit/zipball/v0.2.17rc2
tar zxvf v0.1.6rc2
mv chaoslawful-lua-nginx-module-ccaf132 lua_nginx_module
tar zxvf v0.2.17rc2
mv simpl-ngx_devel_kit-bc97eea ngx_devel_kit
tar zxvf pcre-8.12.tar.gz
tar zxvf nginx-1.0.3.tar.gz
cd nginx-1.0.3
./configure --prefix=/data/soft/nginx --with-pcre=../pcre-8.12 --add-module=../ngx_devel_kit --add-module=../lua_nginx_module
make && make install

安装完成后,我们体验一下lua

第一个lua脚本

ngx.say 是打印的打印输出的意思。。。

location /echo {
    default_type text/plain;
    echo hello lua;
}
location /lua {
    default_type text/plain;
    content_by_lua 'ngx.say("hello world")';
}

用lua脚本做nginx的访问的限制…

……

乐果   发表于   2017 年 09 月 29 日 标签:luanginx 继续阅读

0

4456

nginx+lua+openresty动态二维码生成方案

最近,公司系统与阿里体育对接,在门票业务上我们需要返回二维码图片给对方(可能线下过闸机识别的需要),有几个方案可以选择,公司后端技术栈都集中在php,但用php动态生成效率不高,于是考虑用golang,网上也有一些类库包:github.com/skip2/go-qrcode,但后来发现有人用nginx扩展即实现了,所以我决定也尝试下,顺便熟悉下lua、openresty。

一、安装nginx_lua_module以及echo-nginx-module

Ngx_lua手动编译进Nginx。

首先,我的 Nginx 安装路径为:/usr/local/nginx。

我将尝试编译的两个模块:echo,lua。

所需要的模块如下:

luajit             http://luajit.org 
lua                http://www.lua.org 
ngx_devel_kit      https://github.com/simpl/ngx_devel_kit 
echo-nginx-module  https://github.com/agentzh/echo-nginx-module 
lua-nginx-module   https://github.com/chaoslawful/lua-nginx-module  

前期准备工作

1、安装nginx,版本必须大于1.6,如果已经安装了nginx,则需要重新编译安装下,把conf备份下。

……

乐果   发表于   2017 年 09 月 13 日 标签:nginxlua 继续阅读

较旧的文章
热评文章