Nginx --day11
Nginx配置SSL
Nginx配置示例(单向)
cp /etc/pki/ca_test/server/server.* /usr/local/nginx/conf/
{
listen 443 ssl;
server_name www.aminglinux.com;
index index.html index.php;
root /data/wwwroot/aminglinux.com;
ssl on;
ssl_certificate server.crt;
ssl_certificate_key server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:!eNULL;
ssl_prefer_server_ciphers on;
...
}
配置说明

  1. 443端口为ssl监听端口。
  2. ssl on表示打开ssl支持。
  3. ssl_certificate指定crt文件所在路径,如果写相对路径,必须把该文件和nginx.conf文件放到一个目录下。
  4. ssl_certificate_key指定key文件所在路径。
  5. ssl_protocols指定SSL协议。
  6. ssl_ciphers配置ssl加密算法,多个算法用:分隔,ALL表示全部算法,!表示不启用该算法,+表示将该算法排到最后面去。
  7. ssl_prefer_server_ciphers 如果不指定默认为off,当为on时,在使用SSLv3和TLS协议时,服务器加密算法将优于客户端加密算法。

[root@amingLiunx ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --prefix=/usr/local/nginx/ --add-module=/usr/local/src/echo-nginx-module
[root@amingLiunx ~]# cd nginx-1.14.0
[root@amingLiunx nginx-1.14.0]# ./configure --with-http_ssl_module --prefix=/usr/local/nginx/
[root@amingLiunx nginx-1.14.0]# make install
[root@amingLiunx nginx-1.14.0]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --with-http_ssl_module --prefix=/usr/local/nginx/
[root@amingLiunx nginx-1.14.0]# /usr/local/nginx/sbin/nginx
[root@amingLiunx vhost]# vim www.1.com.conf
server {
listen 443 ssl;
server_name www.123.com;
index index.html;
root /data/wwwroot/www.1.com;
ssl on;
ssl_certificate /etc/pki/ca_test/server/server.crt;
ssl_certificate_key /etc/pki/ca_test/server/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:!eNULL;
ssl_prefer_server_ciphers on;
}
记得改hosts
Nginx配置双向认证(平时几乎用不到,了解过程,帮助理解)
cp /etc/pki/ca_test/root/ca.crt /usr/local/nginx/conf/
配置示例:
{
listen 443 ssl;
server_name www.aminglinux.com;
index index.html index.php;
root /data/wwwroot/aminglinux.com;
ssl on;
ssl_certificate server.crt;
ssl_certificate_key server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:!eNULL;
ssl_prefer_server_ciphers on;
ssl_client_certificate ca.crt; //这里的ca.crt是根证书公钥文件
ssl_verify_client on;
...
}
[root@amingLiunx vhost]# vim www.1.com.conf
server {
listen 443 ssl;
server_name www.123.com;
index index.html;
root /data/wwwroot/www.1.com;
ssl on;
ssl_certificate /etc/pki/ca_test/server/server.crt;
ssl_certificate_key /etc/pki/ca_test/server/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:!eNULL;
ssl_prefer_server_ciphers on;
ssl_client_certificate /etc/pki/ca_test/root/ca.crt;
ssl_verify_client on;
}
客户端(浏览器)操作
如果不进行以下操作,浏览器会出现400错误。400 Bad Request(No required SSL certificate was sent)
首先需要将client.key转换为pfx(p12)格式

cd /etc/pki/ca_test/client

openssl pkcs12 -export -inkey client.key -in client.crt -out client.pfx //这一步需要输入一个自定义密码,一会在windows上安装的时候要用到,需要记一下。

然后将client.pfx拷贝到windows下,双击即可安装。 ||这一步用的是sz client.pfx
也可以直接curl测试:
curl -k --cert /etc/pki/ca_test/client/client.crt --key /etc/pki/ca_test/clien
[root@amingLiunx client]# tail -1 /etc/hosts
127.0.0.1 www.123.com
[root@amingLiunx client]# curl -k --cert /etc/pki/ca_test/client/client.crt --key /etc/pki/ca_test/client/client.key https://www.123.com
good good study!
day day up!!!
Nginx的错误日志
Nginx错误日志平时不用太关注,但是一旦出了问题,就需要借助错误日志来判断问题所在。
配置参数格式:error_log /path/to/log level;
Nginx错误日志级别
常见的错误日志级别有debug | info | notice | warn | error | crit | alert | emerg
级别越高记录的信息越少,如果不定义,默认级别为error.
它可以配置在main、http、server、location段里。
如果在配置文件中定义了两个error_log,在同一个配置段里的话会产生冲突,所以同一个段里只允许配置一个error_log。
但是,在不同的配置段中出现是没问题的。
Nginx错误日志示例
error_log /var/log/nginx/error.log crit;
如果要想彻底关闭error_log,需要这样配置
error_log /dev/null;
小结论:如果同时在主配置文件里和虚拟主机的配置文件里同时打开错误日志,那么访问主机的日志,会在虚拟主机的配置文件日志里生成
Nginx访问日志格式
Nginx访问日志可以设置自定义的格式,来满足特定的需求。
访问日志格式示例
示例1
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' ||加上$http_x_forwarded_for选项的目的是,记录真实的远端地址和代理服务器的地址,而$remote_addr 只能记录代理服务器的地址
'$host "$request_uri" $status'
'"$http_referer" "$http_user_agent"';
示例2
log_format main '$remote_addr [$time_local] '
'$host "$request_uri" $status "$request"'
'"$http_referer" "$http_user_agent" "$request_time"';
若不配置log_format或者不在access_log配置中指定log_format,则默认格式为:
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent";
常见变量
变量说明$time_local通用日志格式下的本地时间;(服务器时间)$remote_addr客户端(用户)IP地址$status请求状态码,如200,404,301,302等$body_bytes_sent发送给客户端的字节数,不包括响应头的大小$bytes_sent发送给客户端的总字节数$request_length请求的长度(包括请求行,请求头和请求正文)$request_time请求处理时间,单位为秒,小数的形式$upstream_addr集群轮询地址$upstream_response_time指从Nginx向后端(php-cgi)建立连接开始到接受完数据然后关闭连接为止的时间$remote_user用来记录客户端用户名称$request请求方式(GET或者POST等)+URL(包含$request_method,$host,$request_uri)$http_user_agent用户浏览器标识$http_host请求的url地址(目标url地址)的host$host等同于$http_host$http_referer来源页面,即从哪个页面转到本页,如果直接在浏览器输入网址来访问,则referer为空$uri请求中的当前URI(不带请求参数,参数位于$args),不同于浏览器传递的$request_uri的值,它可以通过内部重定向,或者使用index指令进行修改。$document_uri等同于$uri$request_uri比$uri多了参数,即$uri+$args$http_x_forwarded_for如果使用了代理,这个参数会记录代理服务器的ip和客户端的ip