openresty

openresty,第1张

注意 : 请求的 url 匹配 listen 端口和 server_name ,如果能匹配端口但是没有 server_name 与之对应的,会匹配第一个 listen 端口,忽视server_name,如 上面的配置,直接访问 http://ip 会跳转到 https://www.m.com

注意 :chrome浏览器在开发者模式选中 disable cache 情况下,301跳转仍然继续会用 cache from disk ,需手动清除缓存 ctrl + shift +delete ,chrome的缓存可通过 chrome://net-internals/ 查看。

在网站全站HTTPS后,如果用户手动敲入网站的HTTP地址,或者从其它地方点击了网站的HTTP链接,通常依赖于服务器端的301/302重定向跳转才能使用HTTPS服务。而第一次的HTTP请求就有可能被劫持,导致请求无法到达服务器,从而构成HTTPS降级劫持。这个问题目前可以通过HSTS(HTTP Strict Transport Security,RFC6797)来解决。

add_header Strict-Transport-Security "max-age=63072000includeSubdomainspreload"

参考 https://www.cnblogs.com/AloneSword/p/4656492.html

TLS1.3是一种新的加密协议,我们把使互联网实现安全通信的基础性技术称为传输层安全协议(TLS)。TLS是安全套接层协议(SSL)的进化版本,SSL是由Netscape公司在1990年代研发的。

参考 https://www.jianshu.com/p/365cb6057387

第1步 - 下载OpenResty的源代码和依赖关系

在本节中,我们将从源代码安装OpenResty。首先,从OpenResty网站的下载页面找到最新的OpenResty源代码版本。下载tarball,确保如果更改了版本号,请使用最新版本号。

wget https://openresty.org/download/openresty-1.11.2.2.tar.gz下载PGP密钥文件,以便我们可以验证文件的内容。

wget https://openresty.org/download/openresty-1.11.2.2.tar.gz.asc接下来,我们需要添加作者的公共密钥,如下载页面上所列。在撰写本文时,这是公钥A0E98066 。但是,请检查它是否已更改它被列在同一下载页面上。

gpg --keyserver pgpkeys.mit.edu --recv-key A0E98066

第2步 - 安装OpenResty

我们将配置OpenResty与PCRE正则表达式和IPv6支持。我们还将通过提供-j2标志来并行化构建过程的一部分,这将告诉make 2个作业可以同时运行。此命令将主要测试所有依赖项是否可用于您的系统,并收集将由构建步骤稍后使用的信息。它也已经构建了一些依赖项,如LuaJIT。./configure -j2 --with-pcre-jit --with-ipv6然后,您可以通过提供-j2并行度标志来构建OpenResty。这将编译OpenResty本身。make -j2最后,您可以安装OpenResty。使用sudo确保所有文件可以复制到系统上的正确位置,以便OpenResty可以在运行时找到它们。sudo make install您需要在防火墙中允许HTTP连接才能使Web服务器正常工作。sudo ufw allow http您也可以选择允许HTTPS与sudo ufw allow https如果你要使用它。您可以通过检查防火墙的状态来验证防火墙的更改。sudo ufw status您应该看到显示的输出中允许HTTP流量(端口80 ),以及如果您添加它的HTTPS(端口443 )。

您现在可以检查安装是否有效。首先,启动OpenResty。sudo /usr/local/openresty/bin/openresty如果命令成功,它将立即完成而不输出文本。在这种情况下,您可以在浏览器中访问http:// your_server_ip 。 你会看到一个页面,说欢迎来到OpenResty!确认它已完全安装和工作。您现在可以停止OpenResty服务器。sudo /usr/local/openresty/bin/openresty -s quitOpenResty已安装,但您仍需要配置OpenResty在启动时运行,所以服务器不必手动启动。

第3步 - 将OpenResty设置为服务

在这里,我们将OpenResty设置为一个服务,所以它在启动时自动启动。我们将使用systemd init服务。 您可以阅读此systemd基础教程了解更多信息,以及本单元文件教程 ,了解单元文件的具体信息。首先使用nano或您喜欢的文本编辑器创建一个新的systemd文件。sudo nano /etc/systemd/system/openresty.service对于本教程,我们将从全新安装中复制默认的Nginx systemd文件,并针对OpenResty进行修改。

第4步 - 配置OpenResty

要配置OpenResty,我们使用默认的Nginx配置作为参考,以便它大部分匹配你可能会熟悉的。 首先,再次打开OpenResty配置文件: sudo nano /usr/local/openresty/nginx/conf/nginx.conf这一次,我们将修改http块并将此http块中的server块移动到一个新文件以具有更好的结构。 首先,找到http {行,并删除之后的一切,除了最后一行与对应的} 。

当前/usr/local/openresty/nginx/conf/nginx.conf

user www-dataworker_processes  autopid /run/openresty.pidevents {worker_connections  1024}http {include       mime.typesdefault_type  application/octet-stream. . .}然后,将以下内容复制到http块中,以便整个文件看起来像这样。我们将一次过一个更改。

/usr/local/openresty/nginx/conf/nginx.conf

user www-dataworker_processes  autopid /run/openresty.pidevents {worker_connections  1024}http {include       mime.typesdefault_type  application/octet-streamsendfile        ontcp_nopush      ontcp_nodelay     onkeepalive_timeout  65ssl_protocols TLSv1 TLSv1.1 TLSv1.2# Dropping SSLv3, ref: POODLEssl_prefer_server_ciphers onaccess_log /var/log/openresty/access.logerror_log /var/log/openresty/error.loggzip  ongzip_disable "msie6"include ../sites/*}保存并关闭文件。 我们对默认文件所做的更改是:

取消tcp_nopush on,它告诉OpenResty只发送完整的数据包。 当使用sendfile选项时,此选项很有用,这将允许OpenResty优化将静态文件发送到客户端。

添加tcp_nodelay on。 此选项将尝试尽快发送数据包,这可能看起来与上述选项相反,但它在不同的时间使用。 tcp_nodelay仅在对HTTP请求使用keepalive选项时使用,这是通过Web浏览器连接到Web服务器,这将避免每次请求时启动HTTP连接的开销。

添加和修改ssl_protocols和ssl_prefer_server_ciphers行。这些选项配置OpenResty的SSL选项。我们删除了易受已知的HTTPS攻击的旧协议,例如POODLE攻击。

添加access_log和error_log行,它们配置Web服务器的日志位置。 我们将日志存储在上一步中创建的/var/log/openresty目录中。

取消注释gzip on并添加gzip_disable "msie6" 。这些选项将配置GZIP,这将压缩网页,以便有更少的数据传输。我们还添加了最后一个选项,因为Internet Explorer 6(及更早版本)并不总是正确处理GZIP内容。

添加include ../sites/*,它告诉OpenResty在/usr/local/openresty/nginx/sites目录中查找额外的配置文件,稍后我们将创建它。

删除所有server块,我们将在此步骤中稍后重新定位到新文件。

接下来,创建我们在include行中指定的新sites目录。 sudo mkdir /usr/local/openresty/nginx/sites

创建default网站。 sudo nano /usr/local/openresty/nginx/sites/default.conf

在此新文件中添加以下内容。这是从nginx.conf重新定位原始服务器块,但有更多细节的内联注释。

/usr/local/openresty/nginx/sites/default.conf

server {

# Listen on port 80.

listen 80 default_server

listen [::]:80 default_server

# The document root.

root /usr/local/openresty/nginx/html/default

# Add index.php if you are using PHP.

index index.html index.htm

# The server name, which isn't relevant in this case, because we only have one.

server_name _

# When we try to access this site...

location / {

# ... first attempt to serve request as file, then as a directory,

# then fall back to displaying a 404.

try_files $uri $uri/ =404

}

# Redirect server error pages to the static page /50x.html.

error_page   500 502 503 504  /50x.html

location = /50x.html {

root /usr/local/openresty/nginx/html

}}

保存并关闭文件。 现在,为此网站创建一个新目录。 sudo mkdir /usr/local/openresty/nginx/html/default

然后将原始index.html从其原始位置移动到新目录。 sudo mv /usr/local/openresty/nginx/html/index.html /usr/local/openresty/nginx/html/default

最后,重新启动OpenResty以使用此新站点。 sudo systemctl restart openresty

您现在可以再次访问http:// your_server_ip ,并查看与之前相同的网页。 现在OpenResty是完全配置的,我们可以尝试一些由OpenResty介绍的,在Nginx默认情况下不可用的功能。

第5步 - 使用OpenResty Lua模块

在本节中,我们将看看OpenResty添加的不同模块的组合,这些模块都存在以适应Lua脚本。我们将在整个步骤中/usr/local/openresty/nginx/sites/default.conf /usr/local/openresty/nginx/sites/default.conf,因此首先打开它。 sudo nano /usr/local/openresty/nginx/sites/default.conf

首先,我们将看一下content_by_lua_block配置选项。 从下面的示例配置中复制location块,并将其添加到server块中,位于两个现有location块下面。

/usr/local/openresty/nginx/sites/default.conf content_by_lua_block示例

server {

. . .

location /example {

default_type 'text/plain'

content_by_lua_block {

ngx.say('Hello, Sammy!')

}

}}

保存并关闭文件,然后重新加载配置。 sudo systemctl reload openresty

如果您http:// your_server_ip /example访问http:// your_server_ip /example ,您会看到一个http:// your_server_ip /example 您好,Sammy的页面! 。让我们解释这是如何工作的。 content_by_lua_block配置指令执行其中的所有内容作为Lua代码。 在这里,我们使用Lua函数ngx.say来打印消息Hello,Sammy!到页面。 对于另一个示例,将location /example块的内容替换为:

/usr/local/openresty/nginx/sites/default.conf content_by_lua_file示例

server {

. . .

location /example {

default_type 'text/plain'

content_by_lua_file /usr/local/openresty/nginx/html/default/index.lua

}}

content_by_lua_file从外部文件加载Lua内容,所以让我们创建上面指定的内容: /usr/local/openresty/nginx/html/default/index.lua 。 sudo nano /usr/local/openresty/nginx/html/default/index.lua

将以下内容添加到文件,然后保存并将其关闭。

/usr/local/openresty/nginx/html/default/index.lua

local name = ngx.var.arg_name or "Anonymous"ngx.say("Hello, ", name, "!")

这是一个简单的Lua,它读取URL中的查询参数, name并自定义问候消息。如果没有传递参数,则使用“匿名”。 再次重新加载配置。 sudo systemctl reload openresty

现在,在浏览器中访问http:// your_server_ip /example?name= Sammy 。 这将显示你好,Sammy! 。 您可以更改name查询参数,或完全忽略它。 Hello, Sammy!

您还可以更改name查询参数以显示任何其他名称。 警告:请勿将您要加载的Lua文件从Web访问到的位置。如果这样做,如果有人访问此文件,则可能会包含应用程序代码。将文件放在文档根目录之外,例如,将文档根目录更改为/usr/local/openresty/nginx/html/default/public ,并将Lua文件放在其上一个目录。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/8352462.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-15
下一篇 2023-04-15

发表评论

登录后才能评论

评论列表(0条)

保存