nginx学习与使用
安装与运行
(从源码安装,这里OS为Ubuntu,参考资料:https://nginx.org/en/docs/configure.html)
1、下载并解压:https://nginx.org/en/download.html
2、安装依赖:PCRE、zlib、OpenSSL(否则安装时会报错,参考:https://blog.csdn.net/somanlee/article/details/69808788)
PCRE: sudo apt-get install libpcre3 libpcre3-dev ,编译nginx所需的pcre-devel是用PCRE开发的、配置文件nginx.conf中使用正则表达式需要PCRE支持
zlib: sudo apt-get install zlib1g-dev ,若在nginx.conf中配置了gzip on,并指定对于某些类型(content-type)的HTTP响应使用gzip来进行压缩以减少网络传输量,则在编译时就必须把zlib编译进nginx
OpenSSL: sudo apt-get install openssl libssl-dev ,若nginx启用HTTPS 或 想使用MD5、SHA1等散列函数则需要OpenSSL
3、配置:进入解压目录,执行 ./configure ,可以通过 --prefix=${dir_path} 指定安装路径,若未指定则结果:
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
可以看出默认将安装在 /usr/local/nginx 下。
4、编译安装: make && make install
5、运行:(默认将使用conf/nginx.conf里的配置)
${nginx_install_home}/sbin/nginx -h ,查看命令:
zsm@zsmUbuntu:~/software/my-nginx-1.14.$ ./sbin/nginx -h
nginx version: nginx/1.14.
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives] Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /home/zsm/software/my-nginx-1.14./)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
${nginx_install_home}/sbin/nginx -h ,查看版本:
zsm@zsmUbuntu:~/software/my-nginx-1.14.$ sudo ./sbin/nginx -V
nginx version: nginx/1.14.
built by gcc 5.4. (Ubuntu 5.4.-6ubuntu1~16.04.)
configure arguments: --prefix=/home/zsm/software/my-nginx-1.14.
${nginx_install_home}/sbin/nginx -s stop |quit |reload |reopen ,启动、重启等。
- stop — fast shutdown
- quit — graceful shutdown
- reload — reloading the configuration file
- reopen — reopening the log files
如果不是源码安装,配置文件、日志等有默认路径,可以通过 nginx -V 查看。结果示例:
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_gunzip_module --with-http_gzip_static_module --with-threads --with-stream --with-stream_realip_module --with-compat --add-module=/usr/src/nginx-sticky-module
反向代理
proxy_pass转发配置
在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也给代理走。
假设前端访问http://192.168.1.1/proxy/test.html,则四种代理情况如下:
第一种:
location /proxy/ {
proxy_pass http://127.0.0.1/;
}
代理到URL:http://127.0.0.1/test.html 第二种(相对于第一种,最后少一个 / )
location /proxy/ {
proxy_pass http://127.0.0.1;
}
代理到URL:http://127.0.0.1/proxy/test.html 第三种:
location /proxy/ {
proxy_pass http://127.0.0.1/aaa/;
}
代理到URL:http://127.0.0.1/aaa/test.html 第四种(相对于第三种,最后少一个 / )
location /proxy/ {
proxy_pass http://127.0.0.1/aaa;
}
代理到URL:http://127.0.0.1/aaatest.html
location的正则匹配规则
参考:
http://seanlook.com/2015/05/17/nginx-location-rewrite/
注:与上述反向代理示例不同,采用正则匹配时,不论指定的proxy_path末尾是否有 / ,最终地址里都不会包含匹配串之后的部分。
匹配命令:
~ 表示正则匹配,区分大小写
~* 表示正则匹配,不区分大小写
^~ 表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项
= 表示普通字符精确匹配
@ 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files
匹配优先级:
- =前缀的指令严格匹配这个查询。如果找到,停止搜索。
- 所有剩下的常规字符串,最长的匹配。如果这个匹配使用^〜前缀,搜索停止。
- 正则表达式,在配置文件中定义的顺序:按前后位置,匹配到即停止。
- 如果第3条规则产生匹配的话,结果被使用。否则,使用第2条规则的结果。
即:精确匹配 -> 完整路径的普通字符匹配-> 带 ^~ 的普通字符匹配 -> 正则顺序匹配 -> 最长前缀的普通字符匹配(匹配到部分路径)
示例:根据参数里的IP、Port信息访问对应的ip、port:
单纯用proxy_pass不行,因为采用正则匹配时匹配串后的部分不会带到proxy_pass里 //HTTP
location ~ /myapi_(\d+\.\d+\.\d+\.\d+)_(\d+)(.*) {
rewrite /myapi_(\d+\.\d+\.\d+\.\d+)_(\d+)(.*) $3 break;
proxy_pass http://$1:$2;
} //websocket
location ~ /wsapi_(\d+\.\d+\.\d+\.\d+)_(\d+)(.*) {
rewrite /wsapi_(\d+\.\d+\.\d+\.\d+)_(\d+)(.*) /wsapi/$3 break;
proxy_connect_timeout 100s;
proxy_read_timeout 100s;
proxy_send_timeout 100s;
proxy_set_header Connection "upgrade";
proxy_set_header Upgrade $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://$1:$2;
}
负载均衡
(基于反向代理)
示例:
http {
# ... others
upstream java_nodes {
server 10.5.31.15:8081;
server 10.5.6.47:8081;
server 172.20.20.226:8081;
server 10.5.34.187:8081;
}
server {
listen 80;
location / {
proxy_pass $scheme://java_nodes;
}
}
# ... others
}
更多可参阅:http://nginx.org/en/docs/http/load_balancing.html
在负载均衡场景下,有时候需要维护session(让一个浏览器一段时间内一直访问到同一个后台服务),可以借助nginx sticky模块:https://www.linuxidc.com/Linux/2017-09/146776.htm
其他
获取请求者真实IP
经过nginx反向代理后,后端服务在获取请求者的IP时(如java里request.getRemoteAddress())得到的是nginx的地址而不是前端调用者的IP。
解决:(参考 https://my.oschina.net/moooofly/blog/295853)
在nginx的location里加上请求头:
proxy_set_header Host $host; #前端调用者http请求中的host,若前端HTTP未带host头则默认成nginx处理该请求的server_name
proxy_set_header X-Real-IP $remote_addr; #前端调用者真实(网络传输层)IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #同上前端IP,不过没经过一层nginx代理就会在后面加一段当前代理的IP,以逗号分隔
nginx Docker
Docker启动nginx时nginx需要以 dameon off 模式运行。
问题:nginx默认启动是在daemon模式下,故在使用命令docker run -d nginx /usr/sbin/nginx时(若未配置daemon off参数),容器启动nginx后会立刻退出。
解决:使用nginx的前台运行模式,需要在配置文件中加“daemon off"指令,或在启动时加“daemon off;"参数,即nginx -g "dameon off;" ,注意off后面的分号不要忽略。也可以不以前台模式运行,而是在nginx之后再运行一个前台命令,如 nginx && top
原因:
表层原因:当以后台模式(-d)运行 run Dokcer容器时,容器内必须有一个前台进程(如top、tail等)否则容器就会结束退出(如service nginx start默认以后台模式运行),因为Docker觉得没事可做了。因此,要想避免容器运行就退出,可以在多个命令的最后加上top等命令
本质原因:docker 容器默认会把容器内部第一个进程,也就是pid=1的程序作为docker容器是否正在运行的依据,若该程序结束了则认为docker 容器挂了,从而docker容器便会直接退出。docker run的时候把command作为容器内部命令,如果你使用nginx未指定daemon off,那么nginx程序将后台运行,这个时候nginx并不是pid为1的程序,而是bash,这个bash执行了nginx指令后就挂了,所以容器也就退出了。
nginx是个高性能服务器,其一大原因就是执行时是单线程模型的(javascript、redis、python异步编程都是单线程模型),在这种模型下没有现存竞争所以不要考虑资源访问的竞争问题。而单线程模型总是和事件驱动挂钩。
nginx学习与使用的更多相关文章
- Nginx学习回顾总结 部分:
21:46 2015/11/9Nginx学习回顾总结进程间通信,近似于socket通信的的东西:才发现这种通信并不是很难,并不是我想象的那样很多内容,新领域,入门只是几个函数的使用而已.以前猜过是这样 ...
- Nginx学习笔记4 源码分析
Nginx学习笔记(四) 源码分析 源码分析 在茫茫的源码中,看到了几个好像挺熟悉的名字(socket/UDP/shmem).那就来看看这个文件吧!从简单的开始~~~ src/os/unix/Ngx_ ...
- Nginx学习笔记~目录索引
回到占占推荐博客索引 前几天整理了<Docker的学习笔记索引>,受到了很多朋友的关注,今天把Nginx的文章也整理一下,以后将永久更新,像大叔之前的<EF文章系列>,< ...
- Nginx学习系列二Linux下Nginx实现负载均衡
关于在本地虚拟机(VMware 14)下安装Linux同时安装Nginx,请参考Nginx学习系列之搭建环境 1.启动Nginx 在Nginx安装成功的前提下,启动Nginx 已root模式登陆(权限 ...
- Nginx系列0:Nginx学习历程
Nginx学习历程 一.初识Nginx 1.Nginx适用于哪些场景 (1)静态资源服务 通过本地文件系统提供服务 (2)反向代理服务 Nginx的强大性能 缓存 负载均衡 (3)API服务 Open ...
- nginx 学习资料
nginx 学习资料 table th:first-of-type { width: 90px; } table th:nth-of-type(2) { } table th:nth-of-type( ...
- Nginx学习总结
2017年2月23日, 星期四 Nginx学习总结 Nginx是目前比较主流的HTTP反向代理服务器(其企业版提供了基于TCP层的反向代理插件),对于构建大型分布式web应用,具有举足轻重的作用.简单 ...
- nginx 学习笔记(2) nginx新手入门
这篇手册简单介绍了nginx,并提供了一些可以操作的简单的工作.前提是nginx已经被安装到你的服务器上.如果没有安装,请阅读上篇:nginx 学习笔记(1) nginx安装.这篇手册主要内容:1. ...
- Nginx学习---Nginx的详解_【all】
1.1. Nginx简介 1.什么是nginx nginx:静态的,开源的www软件,可以解析静态的小文件(低于1M ),支持高并发占用较发少的资源(3W并发,10个进程,内存150M),跨平台 te ...
- Nginx学习之从零搭建静态资源网站
前言 在某学习网站学习了nginx的安装和使用,以此文记录. 环境准备 安装在VMWare下的Centos虚拟机.由于我这是新装的虚拟机.所以很多插件都没有,这里干脆一次性安装上. wget ...
随机推荐
- Linux系统模式之间的转换
1.默认开机进入文本模式 如果想让开机自动进纯文本模式, 修改/etc/inittab 找到其中的 id:5:initdefault: 这行指示启动时的运行级是5,也就是图形模式 改成3就是文本模式了 ...
- [蓝点ZigBee] Zstack 之点亮OLED液晶 ZigBee/CC2530 视频资料
这一小节主要演示如何在Zstack 下移植液晶驱动,我们选取了目前比较流行的OLED 作为移植目标. 移植关键点 1 修改 GPIO pin, 2 如何将Zstack ...
- Django 自定义模板语法
from django import template from django.utils.safestring import mark_safe register = template.Librar ...
- 转载 转载 转载 数组a[],a,&a之间的区别
通俗理解:内存就是公寓房间,指针就是房间的门牌号,数组就是连续的公寓房间,数组名就是这组连续房间的起始地址,也就是第一个房间的地址. 例如int a[5] a是数组名,也就是第一个房间号 & ...
- Wooden Sticks [POJ1065] [DP]
Description 有N根木棍等待处理.机器在处理第一根木棍时需要准备1分钟,此后遇到长宽都不大于前一根木棍的木棍就不需要时间准备,反之则需要1分钟重新准备.比如木棍按照(3,3).(1,3).( ...
- react-native Execution failed for task ':app:prepareRnReduxReactNativeUpdateUnspecifiedLibrary'报错
详细报错 Could not copy zip entry E:\项目目录\node_modules\react-native-update\android\build\outputs\aar\rea ...
- elasticsearch服务安装采坑
笔者这里下载的elastic search,是5.3.0版本,下载地址:https://www.elastic.co/downloads/past-releases/elasticsearch-5-3 ...
- 关于js的函数
1.获取内容的兼容函数 /* * 一: 获取内容的兼容函数 * setText(obj, str) * 思路: * 1.首先判断浏览器: * 2.如果是IE浏览器,就用innerText: * 3.如 ...
- 本地文件上传GitHub
(1)mkdir 项目名称(2)cd 项目名称(3)git init 把它变成可管理的Git仓库(4)git status 查看状态(5)git add . 点用空格隔开(6)git status ...
- hdu1847 Good Luck in CET-4 Everybody!(巴什博弈)
http://acm.hdu.edu.cn/showproblem.php?pid=1847 从1开始枚举情况,找规律.1先手胜2先手胜3先手败4先手胜5先手胜... n只要能转移到先手败,就可以实现 ...