惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
宝玉的分享
宝玉的分享
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Vulnerabilities – Threatpost
博客园_首页
Engineering at Meta
Engineering at Meta
F
Fortinet All Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
罗磊的独立博客
V
Visual Studio Blog
Know Your Adversary
Know Your Adversary
Hacker News - Newest:
Hacker News - Newest: "LLM"
美团技术团队
L
LINUX DO - 最新话题
The Last Watchdog
The Last Watchdog
博客园 - 三生石上(FineUI控件)
T
Tor Project blog
云风的 BLOG
云风的 BLOG
N
Netflix TechBlog - Medium
MyScale Blog
MyScale Blog
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
I
InfoQ
Last Week in AI
Last Week in AI
V2EX - 技术
V2EX - 技术
量子位
S
Secure Thoughts
L
LangChain Blog
The Hacker News
The Hacker News
H
Help Net Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
小众软件
小众软件
K
Kaspersky official blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
I
Intezer
Vercel News
Vercel News
Hacker News: Ask HN
Hacker News: Ask HN
Cisco Talos Blog
Cisco Talos Blog
Google DeepMind News
Google DeepMind News
S
Securelist
阮一峰的网络日志
阮一峰的网络日志
G
Google Developers Blog
Help Net Security
Help Net Security
Martin Fowler
Martin Fowler
爱范儿
爱范儿
Y
Y Combinator Blog
C
Check Point Blog

夜行人

回家路上 第一期的直播演示项目 震动检测器 正能量 在线参观CodeLab Neverland 发布 CodeLab Adapter 3.3.1 DynamicTable 之 纸糊方向盘 CodeLab DynamicTable: 一个可实施的技术方案 CodeLab Insight 发布 Alpha 版 情人节 Home Assistant 周报 && IoT 周报 (02) Joplin: 关注隐私的 Evernote 开源替代软件 浏览器的未来与 Web 传感器 Home Assistant 周报 && IoT 周报 (01) 百宝箱(01) 论自由 介绍 WebThings Home Assistant 周报 && iot 周报 (00) 百宝箱(00) 毛姆读书心得 传世之作 周末徒步 CodeLab Adapter ❤️ Jupyter/Python 航班 躲雨 夏令营途中 [译]思想--作为一种技术 The future of coding 美国之行 三门问题的程序模拟 从Python转向Pharo https://blog.just4fun.site/post/iot/iot-open-source-projects/ Python异步编程笔记 https://blog.just4fun.site/post/iot/iot-open-source-hardware-community/ 万物积木化开发者社区 CodeLab ❤️ Blender Scratch3技术分析之云变量 API(第7篇) [译]对管道(Pipes)的偏爱 [译]提出正确的问题比得到正确答案更重要 蓝牙设备与Scratch3.0 创建你的第一个Scratch3.0 Extension Scratch3技术分析之项目内部数据(第6篇) Scratch3技术分析之社区 API(第5篇) Scratch3技术分析之User API(第4篇) Scratch3技术分析之项目主页API(第3篇) Scratch3技术分析之静态资源API(第2篇) Scratch3.0、micro:bit与Windows7 https://blog.just4fun.site/post/iot/zerynth-vs-micropython/ 核聚变、方所与半宅空间 可视化编程为何是个糟糕的主意 codelab.club周末聚会 关于codelab.club '下一件大事'是一个房间 Hungry Robot - Eat everything 编程作为一种思考方式 今日简史 史蒂夫·乔布斯传 罗素自选文集 https://blog.just4fun.site/post/edx/tianjin-scratch-ai/ https://blog.just4fun.site/post/edx/richie-cms-openedx/ 徒步武功山 WebUSB与micro:bit 积木化编程与3D场景 夜宿武功山顶 scratch3-adapter接入优必选Alpha系列机器人 https://blog.just4fun.site/post/edx/video-migration-note/ scratch3-adapter重构笔记 https://blog.just4fun.site/post/edx/edx-community-members/ 两种硬件编程风格的比较 使用micro:bit自制PPT翻页笔 柏拉图对话集 scratch3.0 + micro:bit 七月电影放映计划 非营利组织的管理 Screenly--用树莓派让任何屏幕变为可编程的数字标牌 以最佳实践开始你的Django项目 micro:bit与事件驱动 为Scratch3.0设计的插件系统(上篇) OCR应用一例 近两年读过的一些好书 blockly开发之使用python驱动浏览器中的turtle(2) 牛顿新传 文学理论入门 逻辑的引擎 人生的意义 blockly开发之生成并运行js代码(1) blockly开发之hello world(0) micro:bit使用笔记 神器之Termux https://blog.just4fun.site/post/iot/micropython-notes/ Cozmo what is this Scratch的前世今生 下段旅程 我行在远方 爆裂 途中杂记 https://blog.just4fun.site/post/edx/open-edx-startup/ cozmo系列之入门 - 有性格且可编程的机器人 PaperWeekly开发笔记 创业二三事
nginx使用笔记
2016-05-04 · via 夜行人

安装

###ubuntu sudo apt-get install nginx

编译安装

waiting (可以加入其它模块)

配置文件

  • /etc/nginx //所有的配置文件
  • /etc/nginx/sites-available //sites-available存放所有配置文件,无论是非启用
  • /etc/nginx/sites-enabled //软链接到sites-available,这里的符号链接代表启用
  • /var/log/nginx //日志
  • /etc/init.d/ //该目录下有启动脚本
  • /usr/share/nginx/www //默认的虚拟主机的目录
  • /usr/sbin/nginx //启动文件

匹配语法

location匹配命令可以参考: nginx location匹配规则

location 优先级:

  1. =前缀的指令严格匹配这个查询。如果找到,停止搜索。
  2. 所有剩下的常规字符串,最长的匹配。如果这个匹配使用^〜前缀,搜索停止。
  3. 正则表达式,在配置文件中定义的顺序。
  4. 如果第3条规则产生匹配的话,结果被使用。否则,使用第2条规则的结果。

例子:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
location  = / {
  # 只匹配"/".
  # [ configuration A ]
}
location  / {
  # 匹配任何请求,因为所有请求都是以"/"开始
  # 但是更长字符匹配或者正则表达式匹配会优先匹配
  # [ configuration B ]
}
location ^~ /images/ {
  # 匹配任何以 /images/ 开始的请求,并停止匹配 其它location
  # [ configuration C ]
}
location ~* .(gif|jpg|jpeg)$ {
  # 匹配以 gif, jpg, or jpeg结尾的请求.
  # 但是所有 /images/ 目录的请求将由 [Configuration C]处理.
  # [ configuration D ]
}

日常管理

  • sudo /etc/init.d/nginx start //apt安装的启动方式
  • sudo /etc/init.d/nginx restart //重启

用例

一个静态文件服务器

1
2
sudo vim /etc/nginx/sites-available/static_server
sudo ln -s /etc/nginx/sites-available/static_server /etc/nginx/sites-enabled
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
:::text

server {
        client_max_body_size 4G;
        listen  80;  ## listen for ipv4; this line is default and implied
        #server_name static.test.sdk.iwplay.com.tw;
	     root /home/wwj/static_dir; #如果restart nginx没有生效,清理掉/etc/nginx/sites-enabled目录下的default文件
	     location / {
               #auth_basic   "Restricted";  #认证模块
               #auth_basic_user_file /etc/nginx/pass_file;
               autoindex on; #显示索引
               autoindex_exact_size on; #显示大小
		       autoindex_localtime on;  #显示时间
        }

}

添加认证用户:

1
2
3
4
:::text
sudo apt-get install apache2-utils
sudo touch /etc/nginx/pass_file
sudo htpasswd -c -d /etc/nginx/pass_file wwj718

反向代理

假设我们的网站跑在5000端口(flask demo)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
:::text
server {
        listen 80;             #端口
        server_name blog.just4fun.site;   #访问域名
        #root /home/bob/dylan/;
        access_log /tmp/access.log;
        error_log /tmp/access.log;
        location / {
                proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;
                if (!-f $request_filename) {
                        proxy_pass http://127.0.0.1:5000;  #这里是内网应用的端口
                        break;
                }
        }
}

我们也时常反向代理到内网的服务器上,诸如: proxy_pass http://192.168.100.2;

如果内网服务器上有多个网站(server_name),那么外层反向代理服务器需要先把server_name导向内网ip,然后内网服务器的nginx来处理具体请求,server_name写了两次。内网的配置不受影响

流媒体

ps:目前版本的nginx好像支持视频流,或者自己编译添加视频模块

目前 WEB 上主流的视频直播方案有 HLS(HTTP Live Streaming) 和 RTMP,移动 WEB 端目前就只有 HLS 能用

docker镜像

docker

我们可以借助docker来使用使用nginx,借助-v参数,可将宿主机的目录映射到docker里,这样一来网站目录和配置文件都不是问题

sudo docker run -it -p 80:80 -v `pwd`/www:/www -v `pwd`/config:/etc/nginx/sites-enabled  -v `pwd`/logs:/var/log/nginx dockerfile/nginx

解释:

  • -v `pwd`/www:/www,将当前路径下的www目录设置为/www,和server配置的路径对应
  • -v `pwd`/logs:/var/log/nginx,nginx运行时产生的日志
  • -v `pwd`/config:/etc/nginx/sites-enabled,server文件的本地路径,映射到docker容器的nginx配置路径。config目录下放一个文件,名为server,Nginx静态网站配置文件,内容为
1
2
3
4
5
6
7
8
9
:::text
server {
        listen 80;

        root /www;
        index index.html index.htm;

        server_name localhost;
}

参考在Docker下部署Nginx

相关项目

server-configs-nginx

Nginx HTTP server boilerplate configs

nginx-rtmp-module

NGINX-based Media Streaming Server

nginx-boilerplate

Awesome Nginx configuration template

openresty

Turning Nginx into a Full-Fledged Scriptable Web Platform (高效)

ngxtop

Real-time metrics for nginx server

nginxparser

Parses nginx configuration with Pyparsing

Same as other serialization modules also you can export configuration with dump and dumps methods.

VeryNginx

功能强大且拥有对人类友好界面的Nginx,提供防火墙,自定义行为,和统计功能

如果你不想安装 OpenResty,或者你已经有了一个正在工作的 Nginx,你也可以自己手动为 Nginx 编译安装这些模块

遇到问题及解决方案

  • Q1:you should increase server_names_hash_bucket_size: 32
    • A: 在配置文件(/etc/nginx/nginx.conf)的http{}段增加一行配置 : server_names_hash_bucket_size 64;

重定向到https

1
2
3
4
5
server {
  listen 80;
  server_name scratch3v3.codelab.club;
  rewrite ^(.*) https://scratch3v3.codelab.club;
}

tips

  • 检查配置: sudo nginx -t

#参考

文章作者 种瓜

上次更新 2016-05-04