http://nginx.org/en/download.html http://www.openssl.org/ http://www.zlib.NET/ http://www.pcre.org/
openssl :
tar zxvf openssl-fips-2.0.12.tar.gz cd openssl-fips-2.0.12 ./config && make && make installzlib:
tar zxvf zlib-1.2.11.tar.gz cd zlib-1.2.11 ./configure && make && make installpcre:
tar zxvf pcre-8.00.tar.gz cd pcre-8.00 ./configure && make && make installnginx:
tar zxvf nginx-1.8.0.tar.gz cd nginx-1.8.0 ./configure && make && make install查找nginx安装地址
whereis nginx启动:
/usr/local/nginx/sbin/nginx报错:
error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解决: 先找到libpcre.so.1位置
whereis libpcre.so.1做软链接
ln -s /usr/local/lib/libpcre.so.1 /lib64可以顺利启动,访问成功
启动:/usr/local/nginx/sbin/nginx 停止/重新加载:/usr/local/nginx/sbin/nginx -s stop(quit、reload) 验证配置文件是否合法:/usr/local/nginx/sbin/nginx -t 命令帮助:/usr/local/nginx/sbin/nginx -h
proxy_pass 在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走。
server { listen 80; server_name test.abc.com; location /proxy/ { proxy_pass http://127.0.0.1:81/; } }访问http:// test.abc.com/proxy/test.html会被代理到http:// 127.0.0.1:81/test.html
server { listen 80; server_name test.abc.com; location /proxy/ { proxy_pass http://127.0.0.1:81; } }访问http:// test.abc.com/proxy/test.html会被代理到http:// 127.0.0.1:81/proxy/test.html
server { listen 80; server_name test.abc.com; location /proxy/ { proxy_pass http://127.0.0.1:81/test/; } }访问http:// test.abc.com/proxy/test.html会被代理到http:// 127.0.0.1:81/test/test.html
server { listen 80; server_name test.abc.com; location /proxy/ { proxy_pass http://127.0.0.1:81/test; } }访问http:// test.abc.com/proxy/test.html会被代理到http:// 127.0.0.1:81/testtest.html 这个url(???http:// 127.0.0.1:81/test/proxy/test.html???)
proxy_redirect
server { listen 80; server_name test.abc.com; location / { proxy_pass http://127.0.0.1:81; } }这段配置一般情况下都正常,但偶尔会出错, 错误在什么地方呢? 抓包发现服务器给客户端的跳转指令里加了端口号,如 Location: http://test.abc.com:9080/abc.html 。因为nginx服务器侦听的是80端口,所以这样的URL给了客户端,必然会出错.针对这种情况, 加一条proxy_redirect指令: proxy_redirect http://test.abc.com:9080/ / ,把所有“http://test.abc.com:9080/”的内容替换成“/”再发给客户端,就解决了。
server { listen 80; server_name test.abc.com; proxy_redirect http://test.abc.com:9080/ /; location / { proxy_pass http://127.0.0.1:81; } }在代替的字段中可以不写服务器名
upstream 配置ngnix负载均衡。server1负载均衡服务器,server2和server3作为web server
upstream web_servers { server server2:80 weight=5; server server3:80 weight=10; } server { listen server1:80; location / { proxy_pass http://web_servers; } }负载均衡有如下算法:
轮询权重轮询:见上例IP Hash : 需要在upstream的配置中指定“ip_hash;”,每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。fair:需要在upstream的配置中指定“fair;”按后端服务器的响应时间来分配请求,响应时间短的优先分配。URL hash:需要在upstream中配置指定:“hash $request_uri; hash_method crc32;”按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。 另外,在nignx中可以为每个 backend server 指定最大的重试次数和重试时间间隔。所使用的关键字是 max_fails 和 fail_timeout。而backup指的是仅当其他serverdown或者忙时才会请求这台server。报错 error: ‘SHA_DIGEST_LENGTH’ undeclared (first use in this function) 解决方式就是修改在你下载解压缩之后的sticky模块文件夹中的ngx_http_sticky_misc.c文件,添加#include <openssl/sha.h>#include <openssl/md5.h>
#include <nginx.h> #include <ngx_config.h> #include <ngx_core.h> #include <ngx_http.h> #include <ngx_md5.h> #include <ngx_sha1.h> #include <openssl/sha.h> #include <openssl/md5.h> #include "ngx_http_sticky_misc.h"再次编译
[root@localhost nginx-1.14.2]make & make install [root@localhost nginx-1.14.2]/usr/local/nginx/sbin/nginx -V