diy 你的nginx-OpenResty

简介: diy 你的nginx-OpenResty

OpenResty

OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。

安装

openresty依赖库为:perl 5.6.1+, libreadline, libpcre, libssl

centos:

yum install readline-devel pcre-devel openssl-devel

ubuntu:

apt-get install libreadline-dev libpcre3-dev libssl-dev perl

编译安装:

下载地址:http://openresty.org/cn/download.html

curl -O https://openresty.org/download/openresty-1.17.8.2.tar.gz
tar -zvxf openresty-1.17.8.2.tar.gz 
cd openresty-1.17.8.2
make 
make install

将默认安装到/usr/local/openresty 目录中

使用

创建一个新目录 openrestyTest,以及创建logs和conf目录:

\[tioncico@tioncico-server homeTest\]$ mkdir openrestyTest
\[tioncico@tioncico-server homeTest\]$ cd openrestyTest/
\[tioncico@tioncico-server openrestyTest\]$ mkdir ./logs ./conf
\[tioncico@tioncico-server openrestyTest\]$

logs将存放openresty的日志,conf存放配置文件

在conf中新建一个nginx.conf文件:

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 9000;
        location / {
            default_type text/html;
            content\_by\_lua '
                ngx.say("<p>Hello, World!</p>")
            ';
        }
    }
}

可以看到,配置跟nginx的一致,不过在location块中多了个content_by_lua块,通过这个可以执行lua脚本

启动openresty:

/usr/local/openresty/nginx/sbin/nginx -p ./ -c ./conf/nginx.conf

-p后面为我们的项目目录,也就是 openrestyTest目录中,-c为配置文件路径

访问 ip:9000即可看到输出:

image.png

停止openresty

/usr/local/openresty/nginx/sbin/nginx -p ./ -s stop

重启 openresty

/usr/local/openresty/nginx/sbin/nginx -p ./ -s reload

注意,启动和停止都必须指定-p,否则守护进程找不到pid文件

通过openresty配置项目

nginx.conf写法:

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 9000;
        server_name xxx.easyswoole.cn;
        index index.php index.html index.htm default.php default.htm default.html;
        root /www/wwwroot/homeTest/openrestyTest;
        ################################### 宝塔 php-fpm支持  #################################
        location ~ \[^/\]\\.php(/|$)
        {
            try_files $uri =404;
            fastcgi_pass  unix:/tmp/php-cgi-72.sock;
            fastcgi_index index.php;
            set $real\_script\_name $fastcgi\_script\_name;
            if ($fastcgi\_script\_name ~ "^(.+?\\.php)(/.+)$") {
                  set $real\_script\_name $1;
                  set $path_info $2;
             }
            fastcgi\_param  SCRIPT\_FILENAME    $document\_root$fastcgi\_script_name;
            fastcgi\_param  QUERY\_STRING       $query_string;
            fastcgi\_param  REQUEST\_METHOD     $request_method;
            fastcgi\_param  CONTENT\_TYPE       $content_type;
            fastcgi\_param  CONTENT\_LENGTH     $content_length;
            fastcgi\_param  SCRIPT\_NAME        $fastcgi\_script\_name;
            fastcgi\_param  REQUEST\_URI        $request_uri;
            fastcgi\_param  DOCUMENT\_URI       $document_uri;
            fastcgi\_param  DOCUMENT\_ROOT      $document_root;
            fastcgi\_param  SERVER\_PROTOCOL    $server_protocol;
            fastcgi\_param  REQUEST\_SCHEME     $scheme;
            fastcgi\_param  HTTPS              $https if\_not_empty;
            fastcgi\_param  GATEWAY\_INTERFACE  CGI/1.1;
            fastcgi\_param  SERVER\_SOFTWARE    nginx/$nginx_version;
            fastcgi\_param  REMOTE\_ADDR        $remote_addr;
            fastcgi\_param  REMOTE\_PORT        $remote_port;
            fastcgi\_param  SERVER\_ADDR        $server_addr;
            fastcgi\_param  SERVER\_PORT        $server_port;
            fastcgi\_param  SERVER\_NAME        $server_name;
            # PHP only, required if PHP was built with --enable-force-cgi-redirect
            fastcgi\_param  REDIRECT\_STATUS    200;
            fastcgi\_param SCRIPT\_FILENAME $document\_root$real\_script_name;
            fastcgi\_param SCRIPT\_NAME $real\_script\_name;
            fastcgi\_param PATH\_INFO $path_info;
        }
        ################ 上面都是宝塔fpm支持的写法 ###################################
        ## 这个是额外的openresty定义写法
        location /openrestyTest {
            default_type text/html;
            content\_by\_lua '
                ngx.say("<p>Hello, World!</p>")
            ';
        }
    }
}

在openrestyTest目录新增index.php:

<?php
echo "666";

重启之后,继续访问:

\[tioncico@tioncico-server openrestyTest\]$ curl http://xxx.easyswoole.cn:9000/openrestyTest
<p>Hello, World!</p>
\[tioncico@tioncico-server openrestyTest\]$ curl http://xxx.easyswoole.cn:9000/
666\[tioncico@tioncico-server openrestyTest\]$
目录
相关文章
|
缓存 负载均衡 前端开发
Nginx极简入门(九)Nginx实现动静分离!
前面介绍了Nginx的负载均衡,一般来说,都需要将动态资源和静态资源分开,这样可以很大程度的提升静态资源的访问速度,同时在开过程中也可以让前后端开发并行可以有效的提高开发时间,也可以有些的减少联调时间 。接下来介绍什么是动静分离以及如何使用Nginx实现动静分离。
Nginx极简入门(九)Nginx实现动静分离!
|
安全 架构师 应用服务中间件
Nginx极简入门(五)配置Nginx反向代理
反向代理(Reverse Proxy)则是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器
Nginx极简入门(五)配置Nginx反向代理
|
6月前
|
应用服务中间件 nginx
[nginx]编译安装openresty
[nginx]编译安装openresty
109 6
|
9月前
|
缓存 应用服务中间件 nginx
【Nginx】动静分离
【1月更文挑战第25天】【Nginx】动静分离
|
域名解析 开发框架 Ubuntu
Caddy VS Nginx,谁才是真正的王者
Caddy 2 is a powerful, enterprise-ready, open source web server with automatic HTTPS written in Go.
7581 1
Caddy VS Nginx,谁才是真正的王者
|
缓存 应用服务中间件 编译器
Nginx之Openresty基本使用解读
Nginx之Openresty基本使用解读
|
前端开发 JavaScript 应用服务中间件
Nginx之动静分离解读
Nginx之动静分离解读
|
负载均衡 前端开发 JavaScript
Nginx——Nginx主要应用之动静分离
Nginx——Nginx主要应用之动静分离
Nginx——Nginx主要应用之动静分离
|
前端开发 JavaScript 应用服务中间件
nginx入门攻略(一篇简单掌握nginx)
nginx入门攻略(一篇简单掌握nginx)
nginx入门攻略(一篇简单掌握nginx)
|
数据采集 移动开发 负载均衡
前端Nginx那些事
随着前端变革,Nginx也成为了前端开发工程师必不可少应该具备的一项技能了,那nginx到底起的是吗作用? 其实Nginx一直跟我们息息相关,它既可以作为 Web 服务器,也可以作为负载均衡服务器,具备高性能、高并发连接等
254 0
前端Nginx那些事