Nginx 安装
Nginx 是一款面向性能设计的 HTTP 服务器,能反向代理 HTTP,HTTPS 和邮件相关(SMTP,POP3,IMAP)的协议连接。而且提供了负载均衡以及 HTTP 缓存。它的设计充分使用异步事件模型,削减上下文调度的开销,提升服务器并发能力。采用了模块化设计,提供了丰富模块的第三方模块。javascript
因此关于 Nginx,有这些标签:「异步」「事件」「模块化」「高性能」「高并发」「反向代理」「负载均衡」html
Linux系统:Centos 7 x64
Nginx版本:1.12.2
java
安装
安装依赖
prce(重定向支持)和openssl(https支持,若是不须要https能够不安装。)linux
yum -y install pcre* yum -y install openssl*
CentOS 6.5 我安装的时候是选择的“基本服务器”,默认这两个包都没安装全,因此这两个都运行安装便可。nginx
下载
wget http://nginx.org/download/nginx-1.12.2.tar.gz
# 若是没有安装wget # 下载已编译版本 $ yum install wget
编译安装
而后进入目录编译安装,configure参数说明c++
cd nginx-1.12.2 ./configure
安装报错误的话好比:“C compiler cc is not found”,这个就是缺乏编译环境,安装一下就能够了 yum -y install gcc make gcc-c++ openssl-devel wgetgit
若是没有error信息,就能够执行下边的安装了:正则表达式
make -j4 && make install
nginx测试
运行下面命令会出现两个结果算法
/usr/local/nginx/sbin ./nginx -t
# nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok # nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
开机自启动
编辑 vi /lib/systemd/system/nginx.service 文件,没有建立一个 touch nginx.service 而后将以下内容根据具体状况进行修改后,添加到nginx.service文件中:后端
[Unit]
Description=nginx1.12.2
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
设置开机启动,使配置生效:
systemctl enable nginx.service
运维
服务管理
# 启动 /usr/local/nginx/sbin/nginx # 重启 /usr/local/nginx/sbin/nginx -s reload # 关闭进程 /usr/local/nginx/sbin/nginx -s stop # 平滑关闭nginx /usr/local/nginx/sbin/nginx -s quit # 查看nginx的安装状态, /usr/local/nginx/sbin/nginx -V
关闭防火墙,或者添加防火墙规则就能够测试了
firewall-cmd --state #查看防火墙状态
systemctl stop firewalld.service #中止firewall
systemctl disable firewalld.service #禁止firewall开机启动
或者编辑配置文件:
vi /etc/sysconfig/iptables
添加这样一条开放80端口的规则后保存:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
重启服务便可:
systemctl restart iptables.service #重启防火墙使配置生效
systemctl enable iptables.service #设置防火墙开机启动
重启服务防火墙报错解决
systemctl restart iptables.service
# Redirecting to /bin/systemctl restart iptables.service # Failed to restart iptables.service: Unit iptables.service failed to load: No such file or directory.
在CentOS 7或RHEL 7或Fedora中防火墙由 firewalld 来管理,固然你能够还原传统的管理方式。或则使用新的命令进行管理。
假如采用传统请执行一下命令:
# 传统命令 systemctl stop firewalld systemctl mask firewalld
# 安装命令 yum install iptables-services systemctl enable iptables service iptables restart
nginx卸载
若是经过yum安装,使用下面命令安装。
yum remove nginx
编译安装,删除/usr/local/nginx目录便可
若是配置了自启动脚本,也须要删除。
参数说明
参数 | 说明 |
---|---|
--prefix=<path> |
Nginx安装路径。若是没有指定,默认为 /usr/local/nginx。 |
--sbin-path=<path> |
Nginx可执行文件安装路径。只能安装时指定,若是没有指定,默认为<prefix> /sbin/nginx。 |
--conf-path=<path> |
在没有给定-c选项下默认的nginx.conf的路径。若是没有指定,默认为<prefix> /conf/nginx.conf。 |
--pid-path=<path> |
在nginx.conf中没有指定pid指令的状况下,默认的nginx.pid的路径。若是没有指定,默认为 <prefix> /logs/nginx.pid。 |
--lock-path=<path> |
nginx.lock文件的路径。 |
--error-log-path=<path> |
在nginx.conf中没有指定error_log指令的状况下,默认的错误日志的路径。若是没有指定,默认为 <prefix> /- logs/error.log。 |
--http-log-path=<path> |
在nginx.conf中没有指定access_log指令的状况下,默认的访问日志的路径。若是没有指定,默认为 <prefix> /- logs/access.log。 |
--user=<user> |
在nginx.conf中没有指定user指令的状况下,默认的nginx使用的用户。若是没有指定,默认为 nobody。 |
--group=<group> |
在nginx.conf中没有指定user指令的状况下,默认的nginx使用的组。若是没有指定,默认为 nobody。 |
--builddir=DIR | 指定编译的目录 |
--with-rtsig_module | 启用 rtsig 模块 |
--with-select_module --without-select_module | 容许或不容许开启SELECT模式,若是 configure 没有找到更合适的模式,好比:kqueue(sun os),epoll (linux kenel 2.6+), rtsig(- 实时信号)或者/dev/poll(一种相似select的模式,底层实现与SELECT基本相 同,都是采用轮训方法) SELECT模式将是默认安装模式 |
--with-poll_module --without-poll_module | Whether or not to enable the poll module. This module is enabled by, default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure. |
--with-http_ssl_module | Enable ngx_http_ssl_module. Enables SSL support and the ability to handle HTTPS requests. Requires OpenSSL. On Debian, this is libssl-dev. 开启HTTP SSL模块,使NGINX能够支持HTTPS请求。这个模块须要已经安装了OPENSSL,在DEBIAN上是libssl |
--with-http_realip_module | 启用 ngx_http_realip_module |
--with-http_addition_module | 启用 ngx_http_addition_module |
--with-http_sub_module | 启用 ngx_http_sub_module |
--with-http_dav_module | 启用 ngx_http_dav_module |
--with-http_flv_module | 启用 ngx_http_flv_module |
--with-http_stub_status_module | 启用 "server status" 页 |
--without-http_charset_module | 禁用 ngx_http_charset_module |
--without-http_gzip_module | 禁用 ngx_http_gzip_module. 若是启用,须要 zlib 。 |
--without-http_ssi_module | 禁用 ngx_http_ssi_module |
--without-http_userid_module | 禁用 ngx_http_userid_module |
--without-http_access_module | 禁用 ngx_http_access_module |
--without-http_auth_basic_module | 禁用 ngx_http_auth_basic_module |
--without-http_autoindex_module | 禁用 ngx_http_autoindex_module |
--without-http_geo_module | 禁用 ngx_http_geo_module |
--without-http_map_module | 禁用 ngx_http_map_module |
--without-http_referer_module | 禁用 ngx_http_referer_module |
--without-http_rewrite_module | 禁用 ngx_http_rewrite_module. 若是启用须要 PCRE 。 |
--without-http_proxy_module | 禁用 ngx_http_proxy_module |
--without-http_fastcgi_module | 禁用 ngx_http_fastcgi_module |
--without-http_memcached_module | 禁用 ngx_http_memcached_module |
--without-http_limit_zone_module | 禁用 ngx_http_limit_zone_module |
--without-http_empty_gif_module | 禁用 ngx_http_empty_gif_module |
--without-http_browser_module | 禁用 ngx_http_browser_module |
--without-http_upstream_ip_hash_module | 禁用 ngx_http_upstream_ip_hash_module |
--with-http_perl_module | 启用 ngx_http_perl_module |
--with-perl_modules_path=PATH | 指定 perl 模块的路径 |
--with-perl=PATH | 指定 perl 执行文件的路径 |
--http-log-path=PATH | Set path to the http access log |
--http-client-body-temp-path=PATH | Set path to the http client request body temporary files |
--http-proxy-temp-path=PATH | Set path to the http proxy temporary files |
--http-fastcgi-temp-path=PATH | Set path to the http fastcgi temporary files |
--without-http | 禁用 HTTP server |
--with-mail | 启用 IMAP4/POP3/SMTP 代理模块 |
--with-mail_ssl_module | 启用 ngx_mail_ssl_module |
--with-cc=PATH | 指定 C 编译器的路径 |
--with-cpp=PATH | 指定 C 预处理器的路径 |
--with-cc-opt=OPTIONS | Additional parameters which will be added to the variable CFLAGS. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-cc-opt="-I /usr/local/include". If we are using select() and it is necessary to increase the number of file descriptors, then this also can be assigned here: --with-cc-opt="-D FD_SETSIZE=2048". |
--with-ld-opt=OPTIONS | Additional parameters passed to the linker. With the use of the system library PCRE in - FreeBSD, it is necessary to indicate --with-ld-opt="-L /usr/local/lib". |
--with-cpu-opt=CPU | 为特定的 CPU 编译,有效的值包括:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64 |
--without-pcre | 禁止 PCRE 库的使用。同时也会禁止 HTTP rewrite 模块。在 "location" 配置指令中的正则表达式也须要 PCRE 。 |
--with-pcre=DIR | 指定 PCRE 库的源代码的路径。 |
--with-pcre-opt=OPTIONS | Set additional options for PCRE building. |
--with-md5=DIR | Set path to md5 library sources. |
--with-md5-opt=OPTIONS | Set additional options for md5 building. |
--with-md5-asm | Use md5 assembler sources. |
--with-sha1=DIR | Set path to sha1 library sources. |
--with-sha1-opt=OPTIONS | Set additional options for sha1 building. |
--with-sha1-asm | Use sha1 assembler sources. |
--with-zlib=DIR | Set path to zlib library sources. |
--with-zlib-opt=OPTIONS | Set additional options for zlib building. |
--with-zlib-asm=CPU | Use zlib assembler sources optimized for specified CPU, valid values are: pentium, pentiumpro |
--with-openssl=DIR | Set path to OpenSSL library sources |
--with-openssl-opt=OPTIONS | Set additional options for OpenSSL building |
--with-debug | 启用调试日志 |
--add-module=PATH | Add in a third-party module found in directory PATH |
配置
在Centos 默认配置文件在 /usr/local/nginx/conf/nginx.conf 咱们要在这里配置一些文件。nginx.conf是主配置文件,由若干个部分组成,每一个大括号{}
表示一个部分。每一行指令都由分号结束;
,标志着一行的结束。
配置文件
nginx 的配置系统由一个主配置文件和其余一些辅助的配置文件构成。这些配置文件均是纯文本文件,所有位于 nginx 安装目录下的 conf 目录下。
指令由 nginx 的各个模块提供,不一样的模块会提供不一样的指令来实现配置。
指令除了 Key-Value 的形式,还有做用域指令。
nginx.conf 中的配置信息,根据其逻辑上的意义,对它们进行了分类,也就是分红了多个做用域,或者称之为配置指令上下文。不一样的做用域含有一个或者多个配置项。
下面的这些上下文指令是用的比较多:
Directive | Description | Contains Directive |
---|---|---|
main | nginx 在运行时与具体业务功能(好比 http 服务或者 email 服务代理)无关的一些参数,好比工做进程数,运行的身份等。 | user, worker_processes, error_log, events, http, mail |
http | 与提供 http 服务相关的一些配置参数。例如:是否使用 keepalive 啊,是否使用 gzip 进行压缩等。 | server |
server | http 服务上支持若干虚拟主机。每一个虚拟主机一个对应的 server 配置项,配置项里面包含该虚拟主机相关的配置。在提供 mail 服务的代理时,也能够创建若干 server. 每一个 server 经过监听的地址来区分。 | listen, server_name, access_log, location, protocol, proxy, smtp_auth, xclient |
location | http 服务中,某些特定的 URL 对应的一系列配置项。 | index, root |
实现 email 相关的 SMTP/IMAP/POP3 代理时,共享的一些配置项(由于可能实现多个代理,工做在多个监听地址上)。 | server, http, imap_capabilities | |
include | 以便加强配置文件的可读性,使得部分配置文件能够从新使用。 | - |
valid_referers | 用来校验Http请求头Referer是否有效。 | - |
try_files | 用在server部分,不过最多见的仍是用在location部分,它会按照给定的参数顺序进行尝试,第一个被匹配到的将会被使用。 | - |
if | 当在location块中使用if指令,在某些状况下它并不按照预期运行,通常来讲避免使用if指令。 | - |
例如咱们再 nginx.conf 里面引用两个配置 vhost/example.com.conf 和 vhost/gitlab.com.conf 它们都被放在一个我本身新建的目录 vhost 下面。nginx.conf 配置以下:
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } include vhost/example.com.conf; include vhost/gitlab.com.conf; }
简单的配置: example.com.conf
server { #侦听的80端口 listen 80; server_name baidu.com app.baidu.com; # 这里指定域名 index index.html index.htm; # 这里指定默认入口页面 root /home/www/app.baidu.com; # 这里指定目录 }
内置预约义变量
Nginx提供了许多预约义的变量,也能够经过使用set来设置变量。你能够在if中使用预约义变量,也能够将它们传递给代理服务器。如下是一些常见的预约义变量,更多详见
变量名称 | 值 |
---|---|
$args_name | 在请求中的name参数 |
$args | 全部请求参数 |
$query_string | $args的别名 |
$content_length | 请求头Content-Length的值 |
$content_type | 请求头Content-Type的值 |
$host | 若是当前有Host,则为请求头Host的值;若是没有这个头,那么该值等于匹配该请求的server_name的值 |
$remote_addr | 客户端的IP地址 |
$request | 完整的请求,从客户端收到,包括Http请求方法、URI、Http协议、头、请求体 |
$request_uri | 完整请求的URI,从客户端来的请求,包括参数 |
$scheme | 当前请求的协议 |
$uri | 当前请求的标准化URI |
反向代理
反向代理是一个Web服务器,它接受客户端的链接请求,而后将请求转发给上游服务器,并将从服务器获得的结果返回给链接的客户端。下面简单的反向代理的例子:
server { listen 80; server_name localhost; client_max_body_size 1024M; # 容许客户端请求的最大单文件字节数 location / { proxy_pass http://localhost:8080; proxy_set_header Host $host:$server_port; } }
复杂的配置: gitlab.com.conf。
server { #侦听的80端口 listen 80; server_name git.example.cn; location / { proxy_pass http://localhost:3000; #如下是一些反向代理的配置可删除 proxy_redirect off; #后端的Web服务器能够经过X-Forwarded-For获取用户真实IP proxy_set_header Host $host; client_max_body_size 10m; #容许客户端请求的最大单文件字节数 client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数 proxy_connect_timeout 300; #nginx跟后端服务器链接超时时间(代理链接超时) proxy_send_timeout 300; #后端服务器数据回传时间(代理发送超时) proxy_read_timeout 300; #链接成功后,后端服务器响应时间(代理接收超时) proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k如下的话,这样设置 proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2) } }
代理到上游服务器的配置中,最重要的是proxy_pass指令。如下是代理模块中的一些经常使用指令:
指令 | 说明 |
---|---|
proxy_connect_timeout | Nginx从接受请求至链接到上游服务器的最长等待时间 |
proxy_send_timeout | 后端服务器数据回传时间(代理发送超时) |
proxy_read_timeout | 链接成功后,后端服务器响应时间(代理接收超时) |
proxy_cookie_domain | 替代从上游服务器来的Set-Cookie头的domain属性 |
proxy_cookie_path | 替代从上游服务器来的Set-Cookie头的path属性 |
proxy_buffer_size | 设置代理服务器(nginx)保存用户头信息的缓冲区大小 |
proxy_buffers | proxy_buffers缓冲区,网页平均在多少k如下 |
proxy_set_header | 重写发送到上游服务器头的内容,也能够经过将某个头部的值设置为空字符串,而不发送某个头部的方法实现 |
proxy_ignore_headers | 这个指令禁止处理来自代理服务器的应答。 |
proxy_intercept_errors | 使nginx阻止HTTP应答代码为400或者更高的应答。 |
负载均衡
upstream指令启用一个新的配置区段,在该区段定义一组上游服务器。这些服务器可能被设置不一样的权重,也可能出于对服务器进行维护,标记为down。
upstream gitlab { ip_hash; server 192.168.122.11:8081 ; server 127.0.0.1:3000; server 127.0.0.1:3001 down; keepalive 32; } server { #侦听的80端口 listen 80; server_name git.example.cn; location / { proxy_pass http://gitlab; #在这里设置一个代理,和upstream的名字同样 #如下是一些反向代理的配置可删除 proxy_redirect off; #后端的Web服务器能够经过X-Forwarded-For获取用户真实IP proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; #容许客户端请求的最大单文件字节数 client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数 proxy_connect_timeout 300; #nginx跟后端服务器链接超时时间(代理链接超时) proxy_send_timeout 300; #后端服务器数据回传时间(代理发送超时) proxy_read_timeout 300; #链接成功后,后端服务器响应时间(代理接收超时) proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffers 4 32k;# 缓冲区,网页平均在32k如下的话,这样设置 proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 } }
每一个请求按时间顺序逐一分配到不一样的后端服务器,若是后端服务器down掉,能自动剔除。
负载均衡:
upstream模块可以使用3种负载均衡算法:轮询、IP哈希、最少链接数。
轮询: 默认状况下使用轮询算法,不须要配置指令来激活它,它是基于在队列中谁是下一个的原理确保访问均匀地分布到每一个上游服务器;
IP哈希: 经过ip_hash指令来激活,Nginx经过IPv4地址的前3个字节或者整个IPv6地址做为哈希键来实现,同一个IP地址老是能被映射到同一个上游服务器;
最少链接数: 经过least_conn指令来激活,该算法经过选择一个活跃数最少的上游服务器进行链接。若是上游服务器处理能力不一样,能够经过给server配置weight权重来讲明,该算法将考虑到不一样服务器的加权最少链接数。
RR
简单配置 ,这里我配置了2台服务器,固然其实是一台,只是端口不同而已,而8081的服务器是不存在的,也就是说访问不到,可是咱们访问 http://localhost
的时候,也不会有问题,会默认跳转到http://localhost:8080
具体是由于Nginx会自动判断服务器的状态,若是服务器处于不能访问(服务器挂了),就不会跳转到这台服务器,因此也避免了一台服务器挂了影响使用的状况,因为Nginx默认是RR策略,因此咱们不须要其余更多的设置
upstream test { server localhost:8080; server localhost:8081; } server { listen 81; server_name localhost; client_max_body_size 1024M; location / { proxy_pass http://test; proxy_set_header Host $host:$server_port; } }
负载均衡的核心代码为
upstream test { server localhost:8080; server localhost:8081; }
权重
指定轮询概率,weight和访问比率成正比,用于后端服务器性能不均的状况。 例如
upstream test { server localhost:8080 weight=9; server localhost:8081 weight=1; }
那么10次通常只会有1次会访问到8081,而有9次会访问到8080
ip_hash
上面的2种方式都有一个问题,那就是下一个请求来的时候请求可能分发到另一个服务器,当咱们的程序不是无状态的时候(采用了session保存数据),这时候就有一个很大的很问题了,好比把登陆信息保存到了session中,那么跳转到另一台服务器的时候就须要从新登陆了,因此不少时候咱们须要一个客户只访问一个服务器,那么就须要用iphash了,iphash的每一个请求按访问ip的hash结果分配,这样每一个访客固定访问一个后端服务器,能够解决session的问题。
upstream test { ip_hash; server localhost:8080; server localhost:8081; }
fair
这是个第三方模块,按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backend { fair; server localhost:8080; server localhost:8081; }
url_hash
这是个第三方模块,按访问url的hash结果来分配请求,使每一个url定向到同一个后端服务器,后端服务器为缓存时比较有效。 在upstream中加入hash语句,server语句中不能写入weight等其余的参数,hash_method是使用的hash算法
upstream backend { hash $request_uri; hash_method crc32; server localhost:8080; server localhost:8081; }
以上5种负载均衡各自适用不一样状况下使用,因此能够根据实际状况选择使用哪一种策略模式,不过fair和url_hash须要安装第三方模块才能使用
server指令可选参数:
-
weight:设置一个服务器的访问权重,数值越高,收到的请求也越多;
-
fail_timeout:在这个指定的时间内服务器必须提供响应,若是在这个时间内没有收到响应,那么服务器将会被标记为down状态;
-
max_fails:设置在fail_timeout时间以内尝试对一个服务器链接的最大次数,若是超过这个次数,那么服务器将会被标记为down;
-
down:标记一个服务器再也不接受任何请求;
-
backup:一旦其余服务器宕机,那么有该标记的机器将会接收请求。
keepalive指令:
Nginx服务器将会为每个worker进行保持同上游服务器的链接。
第三方模块安装方法
./configure --prefix=/你的安装目录 --add-module=/第三方模块目录
常见使用场景
跨域问题
在工做中,有时候会遇到一些接口不支持跨域,这时候能够简单的添加add_headers来支持cors跨域。配置以下:
server { listen 80; server_name api.xxx.com; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET,POST,HEAD'; location / { proxy_pass http://127.0.0.1:3000; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; } }
上面更改头信息,还有一种,使用 rewrite 指令重定向URI来解决跨域问题。
upstream test { server 127.0.0.1:8080; server localhost:8081; } server { listen 80; server_name api.xxx.com; location /{ root html; #去请求../html文件夹里的文件 index index.html index.htm; #首页响应地址 } # 用于拦截请求,匹配任何以 /api/开头的地址, # 匹配符合之后,中止往下搜索正则。 location ^~/api/{ # 表明重写拦截进来的请求,而且只能对域名后边的除去传递的参数外的字符串起做用, # 例如www.a.com/proxy/api/msg?meth=1&par=2重写,只对/proxy/api/msg重写。 # rewrite后面的参数是一个简单的正则 ^/api/(.*)$, # $1表明正则中的第一个(),$2表明第二个()的值,以此类推。 rewrite ^/api/(.*)$ /$1 break; # 把请求代理到其余主机 # 其中 http://www.b.com/ 写法和 http://www.b.com写法的区别以下 # 若是你的请求地址是他 http://server/html/test.jsp # 配置一: http://www.b.com/ 后面有“/” # 将反向代理成 http://www.b.com/html/test.jsp 访问 # 配置一: http://www.b.com 后面没有有“/” # 将反向代理成 http://www.b.com/test.jsp 访问 proxy_pass http://test; } }
ssl配置
超文本传输安全协议(缩写:HTTPS,英语:Hypertext Transfer Protocol Secure)是超文本传输协议和SSL/TLS的组合,用以提供加密通信及对网络服务器身份的鉴定。HTTPS链接常常被用于万维网上的交易支付和企业信息系统中敏感信息的传输。HTTPS不该与在RFC 2660中定义的安全超文本传输协议(S-HTTP)相混。
HTTPS 目前已是全部注重隐私和安全的网站的首选,随着技术的不断发展,HTTPS 网站已再也不是大型网站的专利,全部普通的我的站长和博客都可以本身动手搭建一个安全的加密的网站。
查看目前nginx编译选项
sbin/nginx -V
输出下面内容
nginx version: nginx/1.7.8 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx-1.5.1 --with-http_ssl_module --with-http_spdy_module --with-http_stub_status_module --with-pcre
若是依赖的模块不存在,能够输入下面命令从新编译安装。
./configure --user=www --group=www --prefix=/mt/server/nginx --with-http_stub_status_module --with-openssl=/home/nginx-1.8.0/openssl-1.0.0d --without-http-cache --with-http_ssl_module --with-http_gzip_static_module --with-...
HTTPS server
server { listen 443 ssl; server_name localhost; ssl_certificate /usr/local/nginx/conf/vjjhd.crt; ssl_certificate_key /usr/local/nginx/conf/vjjhd.key; # 设置ssl/tls会话缓存的类型和大小。若是设置了这个参数通常是shared,buildin可能会参数内存碎片,默认是none,和off差很少,停用缓存。如shared:SSL:10m表示我全部的nginx工做进程共享ssl会话缓存,官网介绍说1M能够存放约4000个sessions。 ssl_session_cache shared:SSL:1m; # 客户端能够重用会话缓存中ssl参数的过时时间,内网系统默认5分钟过短了,能够设成30m即30分钟甚至4h。 ssl_session_timeout 5m; # 选择加密套件,不一样的浏览器所支持的套件(和顺序)可能会不一样。 # 这里指定的是OpenSSL库可以识别的写法,你能够经过 openssl -v cipher 'RC4:HIGH:!aNULL:!MD5'(后面是你所指定的套件加密算法) 来看所支持算法。 ssl_ciphers HIGH:!aNULL:!MD5; # 设置协商加密算法时,优先使用咱们服务端的加密套件,而不是客户端浏览器的加密套件。 ssl_prefer_server_ciphers on; location / { root html; index index.html index.htm; } }