用Nginx搭建一个具备缓存功能的反向代理服务

在同一台服务器上,使用nginx提供服务,然后使用openresty提供反向代理服务。

参考《Ubuntu 20.04使用源码安装nginx 1.14.0》安装nginx

参考《用Nginx搭建一个可用的静态资源Web服务器》搭建静态资源Web服务器,但是/nginx/conf/nginx.conf里边的内容修改为:

bash 复制代码
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip on;
    gzip_min_length 1;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;

    server {
        # 让nginx只能监控本地发来的8080端口
        listen       127.0.0.1:8080;
        server_name  localhost;
        access_log /nginx/logs/Sea123.access.log;
        location / {
                alias dlib/;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

/nginx/sbin/nginx -c /nginx/conf/nginx.conf使用配置文件/nginx/conf/nginx.conf启动nginx

参考《Linux学习之Ubuntu 20.04在https://openresty.org下载源码安装Openresty 1.19.3.1,使用systemd管理OpenResty服务》安装Openresty,但是/usr/local/openresty/nginx/conf/nginx.conf里边的内容修改为:

bash 复制代码
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    upstream local {
         server 127.0.0.1:8080;
    }
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
                proxy_pass http://local;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf使用/usr/local/openresty/nginx/conf/nginx.conf当成配置启动openresty中的nginx

在Firefox输入ip,然后按下F12,选择网络,刷新页面,然后点击文件为/的记录,可以看到响应头里边的Serveropenresty/1.19.3.1

此文章为10月Day 20学习笔记,内容来源于极客时间《Nginx 核心知识 150 讲》

相关推荐
徒 花1 小时前
Nginx
运维·nginx·云原生
shumeigang2 小时前
nginx实用配置
运维·nginx
檀越剑指大厂2 小时前
【Nginx系列】Tengine:基于 Nginx 的高性能 Web 服务器与反向代理服务器
服务器·前端·nginx
AI分享猿8 小时前
小白学规则编写:雷池 WAF 配置教程,用 Nginx 护住 WordPress 博客
java·网络·nginx
敲不响的键盘9 小时前
Nginx Location匹配与Proxy_pass匹配规则
运维·nginx
努力进修1 天前
跨地域传文件太麻烦?Nginx+cpolar 让本地服务直接公网访问
运维·nginx·cpolar
rit84324991 天前
在Ubuntu上配置Nginx实现开机自启功能
数据库·nginx·ubuntu
一勺菠萝丶1 天前
芋道后端部署后总自己挂?从 Nginx 报错到 OOM Kill 的完整排查与修复(2核2G 服务器实战)
服务器·chrome·nginx
神奇侠20241 天前
基于spring-boot-admin实现对应用、数据库、nginx等监控
java·数据库·nginx
IT小哥哥呀1 天前
Nginx高可用配置实战:负载均衡 + 健康检查 + 动态扩展
运维·nginx·负载均衡·devops·日志分析·openresty·动态扩展