安装与运行

(从源码安装,这里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://www.nginx.cn/115.html

http://seanlook.com/2015/05/17/nginx-location-rewrite/

:与上述反向代理示例不同,采用正则匹配时,不论指定的proxy_path末尾是否有 / ,最终地址里都不会包含匹配串之后的部分。

匹配命令:

~ 表示正则匹配,区分大小写
~* 表示正则匹配,不区分大小写
^~ 表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项
= 表示普通字符精确匹配
@ 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files

匹配优先级:

  1. =前缀的指令严格匹配这个查询。如果找到,停止搜索。
  2. 所有剩下的常规字符串,最长的匹配。如果这个匹配使用^〜前缀,搜索停止。
  3. 正则表达式,在配置文件中定义的顺序:按前后位置,匹配到即停止。
  4. 如果第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学习与使用的更多相关文章

  1. Nginx学习回顾总结 部分:

    21:46 2015/11/9Nginx学习回顾总结进程间通信,近似于socket通信的的东西:才发现这种通信并不是很难,并不是我想象的那样很多内容,新领域,入门只是几个函数的使用而已.以前猜过是这样 ...

  2. Nginx学习笔记4 源码分析

    Nginx学习笔记(四) 源码分析 源码分析 在茫茫的源码中,看到了几个好像挺熟悉的名字(socket/UDP/shmem).那就来看看这个文件吧!从简单的开始~~~ src/os/unix/Ngx_ ...

  3. Nginx学习笔记~目录索引

    回到占占推荐博客索引 前几天整理了<Docker的学习笔记索引>,受到了很多朋友的关注,今天把Nginx的文章也整理一下,以后将永久更新,像大叔之前的<EF文章系列>,< ...

  4. Nginx学习系列二Linux下Nginx实现负载均衡

    关于在本地虚拟机(VMware 14)下安装Linux同时安装Nginx,请参考Nginx学习系列之搭建环境 1.启动Nginx 在Nginx安装成功的前提下,启动Nginx 已root模式登陆(权限 ...

  5. Nginx系列0:Nginx学习历程

    Nginx学习历程 一.初识Nginx 1.Nginx适用于哪些场景 (1)静态资源服务 通过本地文件系统提供服务 (2)反向代理服务 Nginx的强大性能 缓存 负载均衡 (3)API服务 Open ...

  6. nginx 学习资料

    nginx 学习资料 table th:first-of-type { width: 90px; } table th:nth-of-type(2) { } table th:nth-of-type( ...

  7. Nginx学习总结

    2017年2月23日, 星期四 Nginx学习总结 Nginx是目前比较主流的HTTP反向代理服务器(其企业版提供了基于TCP层的反向代理插件),对于构建大型分布式web应用,具有举足轻重的作用.简单 ...

  8. nginx 学习笔记(2) nginx新手入门

    这篇手册简单介绍了nginx,并提供了一些可以操作的简单的工作.前提是nginx已经被安装到你的服务器上.如果没有安装,请阅读上篇:nginx 学习笔记(1) nginx安装.这篇手册主要内容:1. ...

  9. Nginx学习---Nginx的详解_【all】

    1.1. Nginx简介 1.什么是nginx nginx:静态的,开源的www软件,可以解析静态的小文件(低于1M ),支持高并发占用较发少的资源(3W并发,10个进程,内存150M),跨平台 te ...

  10. Nginx学习之从零搭建静态资源网站

    前言   在某学习网站学习了nginx的安装和使用,以此文记录. 环境准备   安装在VMWare下的Centos虚拟机.由于我这是新装的虚拟机.所以很多插件都没有,这里干脆一次性安装上. wget ...

随机推荐

  1. 潭州课堂25班:Ph201805201 django 项目 第四十课 后台 文章发布,更新实现,热门新闻管理,轮播图管理(课堂笔记)

    把图片上传到 七牛云,必须经过后台的许可, 在虚拟机中安装七牛云所需模块pip install qiniu # 创建utils/secrets/qiniu_secret_info.py文件 # 从七牛 ...

  2. 潭州课堂25班:Ph201805201 django 项目 第七课 用户模型设计 (课堂笔记

    在 user 的应用中的 models.py: 导入 dango 自带的用户模型 from django.contrib.auth.models import AbstractUser,UserMan ...

  3. 2-SAT问题的小结

    简介 什么是2-SAT呢?就是有一些集合,每个集合中有且仅有两个元素,且不能同时选取两个元素,集合间的元素存在一定的选择关系,求解可行性及可行方案. 算法 1.连边 2.跑tarjan 3.判可行性, ...

  4. 论文泛读:Click Fraud Detection: Adversarial Pattern Recognition over 5 Years at Microsoft

    这篇论文非常适合工业界的人(比如我)去读,有很多的借鉴意义. 强烈建议自己去读. title:五年微软经验的点击欺诈检测 摘要:1.微软很厉害.2.本文描述了大规模数据挖掘所面临的独特挑战.解决这一问 ...

  5. Servlet(8)—GenericServlet和改进Servlet实例

    概念genericservlet是个抽象的父类,必须给出子类才能实例化它.它给出了设计servlet的一些骨架,定义了servlet生命周期,还有一些得到名字.配置.初始化参数的方法,其设计的是和应用 ...

  6. flask之信号和mateclass元类

    本篇导航: flask实例化参数 信号 metaclass元类解析 一.flask实例化参数 instance_path和instance_relative_config是配合来用的:这两个参数是用来 ...

  7. vue把localhost改成ip地址无法访问—解决方法

    打开package.json文件,找到下面的代码 "scripts": { "dev": "webpack-dev-server --inline - ...

  8. Glide 4.0.0 下之加载本地缓存的图片

    在网上搜了下,无意中发现RequestOptions还有个方法: onlyRetrieveFromCache 用了下是OK的 try { File imageFile = Glide.with(con ...

  9. alpine-bash镜像制作

    alpine轻量级基于busybox的发行版,特别适合基于docker的base images. 特点: small.simple.secure 官方地址: https://alpinelinux.o ...

  10. 怎么样加快JavaScript加载和执行效率

    概览 无论当前 JavaScript 代码是内嵌还是在外链文件中,页面的下载和渲染都必须停下来等待脚本执行完成.JavaScript 执行过程耗时越久,浏览器等待响应用户输入的时间就越长.浏览器在下载 ...