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"
View Code

可以看出默认将安装在 /usr/local/nginx 下。

4、编译安装: make && make install 

5、运行:(默认将使用conf/nginx.conf里的配置)

 ${nginx_install_home}/sbin/nginx -h ,查看命令:

zsm@zsmUbuntu:~/software/my-nginx-1.14.0$ ./sbin/nginx -h
nginx version: nginx/1.14.0
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.0/)
  -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.0$ sudo ./sbin/nginx -V
nginx version: nginx/1.14.0
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10) 
configure arguments: --prefix=/home/zsm/software/my-nginx-1.14.0

 ${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
View Code

 

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
}
View Code

更多可参阅: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异步编程都是单线程模型),在这种模型下没有现存竞争所以不要考虑资源访问的竞争问题。而单线程模型总是和事件驱动挂钩。

 

posted @ 2018-07-26 09:48  March On  阅读(801)  评论(0编辑  收藏  举报
top last
Welcome user from
(since 2020.6.1)