阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx安装配置使用详细笔记

408次阅读
没有评论

共计 19225 个字符,预计需要花费 49 分钟才能阅读完成。

前言:
选择 Nginx 的优点:
Nginx 可以在大多数 Unix like OS 上编译运行,并有 Windows 移植版。Nginx 的 1.4.0 稳定版已经于 2013 年 4 月 24 日发布,一般情况下,对于新建站点,建议使用最新稳定版作为生产版本,已有站点的升级急迫性不高。Nginx 的源代码使用 2-clause BSD-like license。
Nginx 是一个很强大的高性能 Web 和反向代理服务器,它具有很多非常优越的特性:
在高连接并发的情况下,Nginx 是 Apache 服务器不错的替代品:Nginx 在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应,感谢 Nginx 为我们选择了 epoll and kqueue 作为开发模型。

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 搭建视频点播服务器(仿真专业流媒体软件)http://www.linuxidc.com/Linux/2012-08/69151.htm

1.1 执行安装

  1. tar xvf nginx1.4.2.tar.gz
  2. cd nginx1.4.2
  3. ./configure prefix=/usr/nginx withhttp_stub_status_module withdebug withhttp_realip_module withhttp_ssl_module
  4. [root@localhost nginx1.4.2]# make install
  5. ......
  6. test d \‘/usr/nginx/logs\’ || mkdir p \‘/usr/nginx/logs\’
  7. test d \‘/usr/nginx/logs\’ || mkdir p \‘/usr/nginx/logs\’
  8. test d \‘/usr/nginx/html\’ || cp R html \‘/usr/nginx\’
  9. test d \‘/usr/nginx/logs\’ || mkdir p \‘/usr/nginx/logs\’


1.2 查看进程数
进程数是与 top 出来的 cpu 数量是一样的。在 /usr/local/nginx/conf/nginx.conf 配置文件里面的 worker_processes 参数。
worker_processes 指明了 nginx 要开启的进程数,据官方说法,一般开一个就够了,多开几个,可以减少机器 io 带来的影响。据实践表明,nginx 的这个参数在一般情况下开 4 个或 8 个就可以了,再往上开的话优化不太大。据另一种说法是,nginx 开启太多的进程,会影响主进程调度,所以占用的 cpu 会增高。

  1. [root@lbnet2 ~]# ps eaf|grep nginx
  2. root 2221 1382 0 18:06 pts/0 00:00:00 grep nginx
  3. root 16260 1 0 Jun18 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
  4. nobody 16261 16260 0 Jun18 ? 00:01:26 nginx: worker process
  5. nobody 16262 16260 0 Jun18 ? 00:01:32 nginx: worker process
  6. nobody 16263 16260 0 Jun18 ? 00:01:25 nginx: worker process
  7. nobody 16264 16260 0 Jun18 ? 00:01:33 nginx: worker process
  8. nobody 16265 16260 0 Jun18 ? 00:01:32 nginx: worker process
  9. nobody 16266 16260 0 Jun18 ? 00:01:24 nginx: worker process
  10. nobody 16267 16260 0 Jun18 ? 00:01:32 nginx: worker process
  11. nobody 16268 16260 0 Jun18 ? 00:01:23 nginx: worker process
  12. nobody 16269 16260 0 Jun18 ? 00:01:32 nginx: worker process
  13. nobody 16270 16260 0 Jun18 ? 00:01:26 nginx: worker process
  14. nobody 16271 16260 0 Jun18 ? 00:01:32 nginx: worker process
  15. nobody 16272 16260 0 Jun18 ? 00:01:25 nginx: worker process
  16. nobody 16273 16260 0 Jun18 ? 00:01:26 nginx: worker process
  17. nobody 16274 16260 0 Jun18 ? 00:01:32 nginx: worker process
  18. nobody 16275 16260 0 Jun18 ? 00:01:32 nginx: worker process
  19. nobody 16276 16260 0 Jun18 ? 00:01:33 nginx: worker process
  20. nobody 16277 16260 0 Jun18 ? 00:01:24 nginx: worker process
  21. nobody 16278 16260 0 Jun18 ? 00:01:24 nginx: worker process
  22. nobody 16279 16260 0 Jun18 ? 00:01:30 nginx: worker process
  23. nobody 16280 16260 0 Jun18 ? 00:01:24 nginx: worker process
  24. nobody 16281 16260 0 Jun18 ? 00:01:32 nginx: worker process
  25. nobody 16282 16260 0 Jun18 ? 00:01:32 nginx: worker process
  26. nobody 16283 16260 0 Jun18 ? 00:01:25 nginx: worker process
  27. nobody 16284 16260 0 Jun18 ? 00:01:26 nginx: worker process


2 配置文件
2.1 Nginx 反向代理实践
省过

2.2 Nginx Rewrite 重新定向
使用 nginx 做重新定向。
nginx 参考网址:http://blog.sina.com.cn/s/blog_97688f8e0100zws5.html
语法规则:location [=|~|~*|^~] /uri/ {…}
= 开头表示精确匹配
^~ 开头表示 uri 以某个常规字符串开头,理解为匹配 url 路径即可。nginx 不对 url 做编码,因此请求为 /static/20%/aa,可以被规则 ^~ /static/ /aa 匹配到(注意是空格)。
~ 开头表示区分大小写的正则匹配
~* 开头表示不区分大小写的正则匹配
!~ 和!~* 分别为区分大小写不匹配及不区分大小写不匹配 的正则
/ 通用匹配,任何请求都会匹配到。
多个 location 配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):
首先匹配 =,其次匹配 ^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
例子,有如下匹配规则:
location = / {
#规则 A
}
location = /login {
#规则 B
}
location ^~ /static/ {
#规则 C
}
location ~ \.(gif|jpg|png|js|css)$ {
#规则 D
}
location ~* \.png$ {
#规则 E
}
location !~ \.xhtml$ {
#规则 F
}
location !~* \.xhtml$ {
#规则 G
}
location / {
#规则 H
}
那么产生的效果如下:
访问根目录 /,比如 http://localhost/ 将匹配规则 A
访问 http://localhost/login 将匹配规则 B,http://localhost/register 则匹配规则 H
访问 http://localhost/static/a.html 将匹配规则 C
访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则 D 和规则 E,但是规则 D 顺序优先,规则 E 不起作用,而 http://localhost/static/c.png 则优先匹配到规则 C
访问 http://localhost/a.PNG 则匹配规则 E,而不会匹配规则 D,因为规则 E 不区分大小写。
访问 http://localhost/a.xhtml 不会匹配规则 F 和规则 G,http://localhost/a.XHTML 不会匹配规则 G,因为不区分大小写。规则 F,规则 G 属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。
访问 http://localhost/category/id/1111 则最终匹配到规则 H,因为以上规则都不匹配,这个时候应该是 nginx 转发请求给后端应用服务器,比如 FastCGI(php),tomcat(jsp),nginx 作为方向代理服务器存在。

所以实际使用中,个人觉得至少有三个匹配规则定义,如下:
# 直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
# 这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则
location = / {
proxy_pass http://tomcat:8080/index
}
# 第二个必选规则是处理静态文件请求,这是 nginx 作为 http 服务器的强项
# 有两种配置模式,目录匹配或后缀匹配, 任选其一或搭配使用
location ^~ /static/ {
root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
root /webroot/res/;
}
# 第三个规则就是通用规则,用来转发动态请求到后端应用服务器
# 非静态文件请求就默认是动态请求,自己根据实际把握
# 毕竟目前的一些框架的流行,带.php,.jsp 后缀的情况很少了
location / {
proxy_pass http://tomcat:8080/
}

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2014-07/104499p2.htm

2.3 ReWrite 语法
last – 基本上都用这个 Flag。
break – 中止 Rewirte,不在继续匹配
redirect – 返回临时重定向的 HTTP 状态 302
permanent – 返回永久重定向的 HTTP 状态 301
1、下面是可以用来判断的表达式:
- f 和!- f 用来判断是否存在文件
- d 和!- d 用来判断是否存在目录
- e 和!- e 用来判断是否存在文件或目录
- x 和!- x 用来判断文件是否可执行
2、下面是可以用作判断的全局变量
例:http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php

2.4 Redirect 语法
server {
listen 80;
server_name start.igrow.cn;
index index.html index.php;
root html;
if ($http_host !~“^star\.igrow\.cn$&quot {
rewrite ^(.*) http://star.igrow.cn$1 redirect;
}
}

2.5 防盗链
location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
rewrite ^/ http://$host/logo.png;
}
}

2.6 根据文件类型设置过期时间
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
expires 1h;
break;
}
}

2.7 禁止访问某个目录
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test;
deny all;
}
一些可用的全局变量:
$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri


2.8 Nginx 静态文件 (css,js,jpg 等等 web 静态资源)
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
open_file_cache max=10000 inactive=60s;


location /group1/M00 {
root /data/fastdfs/data;
ngx_fastdfs_module;
}


location /css {
root plocc_static;
include gzip.conf;
}


location /common {
root plocc_static;
include gzip.conf;
}


2.9 nginx 转发工程的日志文件
去 nginx.conf 配置文件里面去看访问日志,如下:
vim nginx.conf
location ~* ^/mobileWeb/.*$ {
include deny.conf;


proxy_pass http://mobilewebbackend;
include proxy.conf;


error_log logs/mobileweb_error.log error;
access_log logs/mobileweb_access.log main;


include gzip.conf;
}
再去 logs 目录查看日志文件,如下:
[root@xx logs]# ll /usr/local/nginx/logs/mobileweb*
-rw-r–r–. 1 root root 10946 7 月 18 10:36 /usr/local/nginx/logs/mobileweb_access.log
-rw-r–r–. 1 root root 1628 7 月 18 10:36 /usr/local/nginx/logs/mobileweb_error.log


3 添加启动服务

  1. [root@localhost nginx]# cat /etc/init.d/nginx
  2. #!/bin/bash
  3. #chkconfig:2345 70 70
  4. #description:nginx
  5. BIN=/usr/nginx/sbin/nginx
  6. function d_start {
  7. $BIN || echo n \“nginx is running\”
  8. }
  9. function d_stop {
  10. $BIN s stop || echo n \“nginx is not running\”
  11. }
  12. function d_reload {
  13. $BIN s reload || echo n \“nginx reload failed\”
  14. }
  15. case $1 in
  16. start)
  17. echo start nginx
  18. d_start
  19. ;;
  20. stop)
  21. echo stop nginx
  22. d_stop
  23. ;;
  24. reload)
  25. echo reload nginx
  26. d_reload
  27. ;;
  28. restart)
  29. echo restart nginx
  30. d_stop
  31. echo sleep 5s
  32. sleep 5
  33. d_start
  34. ;;
  35. *)
  36. echo \“Usage: nginx [start | stop |reload |restart]\”
  37. ;;
  38. esac
  39. exit 0

启动:service nginx start;

前言:
选择 Nginx 的优点:
Nginx 可以在大多数 Unix like OS 上编译运行,并有 Windows 移植版。Nginx 的 1.4.0 稳定版已经于 2013 年 4 月 24 日发布,一般情况下,对于新建站点,建议使用最新稳定版作为生产版本,已有站点的升级急迫性不高。Nginx 的源代码使用 2-clause BSD-like license。
Nginx 是一个很强大的高性能 Web 和反向代理服务器,它具有很多非常优越的特性:
在高连接并发的情况下,Nginx 是 Apache 服务器不错的替代品:Nginx 在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应,感谢 Nginx 为我们选择了 epoll and kqueue 作为开发模型。

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 搭建视频点播服务器(仿真专业流媒体软件)http://www.linuxidc.com/Linux/2012-08/69151.htm

1.1 执行安装

  1. tar xvf nginx1.4.2.tar.gz
  2. cd nginx1.4.2
  3. ./configure prefix=/usr/nginx withhttp_stub_status_module withdebug withhttp_realip_module withhttp_ssl_module
  4. [root@localhost nginx1.4.2]# make install
  5. ......
  6. test d \‘/usr/nginx/logs\’ || mkdir p \‘/usr/nginx/logs\’
  7. test d \‘/usr/nginx/logs\’ || mkdir p \‘/usr/nginx/logs\’
  8. test d \‘/usr/nginx/html\’ || cp R html \‘/usr/nginx\’
  9. test d \‘/usr/nginx/logs\’ || mkdir p \‘/usr/nginx/logs\’


1.2 查看进程数
进程数是与 top 出来的 cpu 数量是一样的。在 /usr/local/nginx/conf/nginx.conf 配置文件里面的 worker_processes 参数。
worker_processes 指明了 nginx 要开启的进程数,据官方说法,一般开一个就够了,多开几个,可以减少机器 io 带来的影响。据实践表明,nginx 的这个参数在一般情况下开 4 个或 8 个就可以了,再往上开的话优化不太大。据另一种说法是,nginx 开启太多的进程,会影响主进程调度,所以占用的 cpu 会增高。

  1. [root@lbnet2 ~]# ps eaf|grep nginx
  2. root 2221 1382 0 18:06 pts/0 00:00:00 grep nginx
  3. root 16260 1 0 Jun18 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
  4. nobody 16261 16260 0 Jun18 ? 00:01:26 nginx: worker process
  5. nobody 16262 16260 0 Jun18 ? 00:01:32 nginx: worker process
  6. nobody 16263 16260 0 Jun18 ? 00:01:25 nginx: worker process
  7. nobody 16264 16260 0 Jun18 ? 00:01:33 nginx: worker process
  8. nobody 16265 16260 0 Jun18 ? 00:01:32 nginx: worker process
  9. nobody 16266 16260 0 Jun18 ? 00:01:24 nginx: worker process
  10. nobody 16267 16260 0 Jun18 ? 00:01:32 nginx: worker process
  11. nobody 16268 16260 0 Jun18 ? 00:01:23 nginx: worker process
  12. nobody 16269 16260 0 Jun18 ? 00:01:32 nginx: worker process
  13. nobody 16270 16260 0 Jun18 ? 00:01:26 nginx: worker process
  14. nobody 16271 16260 0 Jun18 ? 00:01:32 nginx: worker process
  15. nobody 16272 16260 0 Jun18 ? 00:01:25 nginx: worker process
  16. nobody 16273 16260 0 Jun18 ? 00:01:26 nginx: worker process
  17. nobody 16274 16260 0 Jun18 ? 00:01:32 nginx: worker process
  18. nobody 16275 16260 0 Jun18 ? 00:01:32 nginx: worker process
  19. nobody 16276 16260 0 Jun18 ? 00:01:33 nginx: worker process
  20. nobody 16277 16260 0 Jun18 ? 00:01:24 nginx: worker process
  21. nobody 16278 16260 0 Jun18 ? 00:01:24 nginx: worker process
  22. nobody 16279 16260 0 Jun18 ? 00:01:30 nginx: worker process
  23. nobody 16280 16260 0 Jun18 ? 00:01:24 nginx: worker process
  24. nobody 16281 16260 0 Jun18 ? 00:01:32 nginx: worker process
  25. nobody 16282 16260 0 Jun18 ? 00:01:32 nginx: worker process
  26. nobody 16283 16260 0 Jun18 ? 00:01:25 nginx: worker process
  27. nobody 16284 16260 0 Jun18 ? 00:01:26 nginx: worker process


2 配置文件
2.1 Nginx 反向代理实践
省过

2.2 Nginx Rewrite 重新定向
使用 nginx 做重新定向。
nginx 参考网址:http://blog.sina.com.cn/s/blog_97688f8e0100zws5.html
语法规则:location [=|~|~*|^~] /uri/ {…}
= 开头表示精确匹配
^~ 开头表示 uri 以某个常规字符串开头,理解为匹配 url 路径即可。nginx 不对 url 做编码,因此请求为 /static/20%/aa,可以被规则 ^~ /static/ /aa 匹配到(注意是空格)。
~ 开头表示区分大小写的正则匹配
~* 开头表示不区分大小写的正则匹配
!~ 和!~* 分别为区分大小写不匹配及不区分大小写不匹配 的正则
/ 通用匹配,任何请求都会匹配到。
多个 location 配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):
首先匹配 =,其次匹配 ^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
例子,有如下匹配规则:
location = / {
#规则 A
}
location = /login {
#规则 B
}
location ^~ /static/ {
#规则 C
}
location ~ \.(gif|jpg|png|js|css)$ {
#规则 D
}
location ~* \.png$ {
#规则 E
}
location !~ \.xhtml$ {
#规则 F
}
location !~* \.xhtml$ {
#规则 G
}
location / {
#规则 H
}
那么产生的效果如下:
访问根目录 /,比如 http://localhost/ 将匹配规则 A
访问 http://localhost/login 将匹配规则 B,http://localhost/register 则匹配规则 H
访问 http://localhost/static/a.html 将匹配规则 C
访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则 D 和规则 E,但是规则 D 顺序优先,规则 E 不起作用,而 http://localhost/static/c.png 则优先匹配到规则 C
访问 http://localhost/a.PNG 则匹配规则 E,而不会匹配规则 D,因为规则 E 不区分大小写。
访问 http://localhost/a.xhtml 不会匹配规则 F 和规则 G,http://localhost/a.XHTML 不会匹配规则 G,因为不区分大小写。规则 F,规则 G 属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。
访问 http://localhost/category/id/1111 则最终匹配到规则 H,因为以上规则都不匹配,这个时候应该是 nginx 转发请求给后端应用服务器,比如 FastCGI(php),tomcat(jsp),nginx 作为方向代理服务器存在。

所以实际使用中,个人觉得至少有三个匹配规则定义,如下:
# 直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
# 这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则
location = / {
proxy_pass http://tomcat:8080/index
}
# 第二个必选规则是处理静态文件请求,这是 nginx 作为 http 服务器的强项
# 有两种配置模式,目录匹配或后缀匹配, 任选其一或搭配使用
location ^~ /static/ {
root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
root /webroot/res/;
}
# 第三个规则就是通用规则,用来转发动态请求到后端应用服务器
# 非静态文件请求就默认是动态请求,自己根据实际把握
# 毕竟目前的一些框架的流行,带.php,.jsp 后缀的情况很少了
location / {
proxy_pass http://tomcat:8080/
}

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2014-07/104499p2.htm

4 制作证书 Key。
4.1. 首先要生成服务器端的私钥 (key 文件):
openssl genrsa -des3 -out server.key 2048

Enter pass phrase for server.key:gongsilong0617

4.2. 用 server.key 生成一个证书:
openssl req -new -key server.key -out server.csr
pass phrase: gongsilong0617

[root@localhost ssl]# openssl req -new -key server.key -out server.csr
Enter pass phrase for server.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
—–
Country Name (2 letter code) [GB]:cn
State or Province Name (full name) [Berkshire]:shanghai
Locality Name (eg, city) [Newbury]:shanghai
Organization Name (eg, company) [My Company Ltd]:gongsilong
Organizational Unit Name (eg, section) []:business
Common Name (eg, your name or your server’s hostname) []:ops
Email Address []:mch@gongsilong.com

Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:gongsilong0617
An optional company name []:gongsilong
[root@localhost ssl]#

4.3. 对客户端也作同样的命令生成 key 及 csr 文件
openssl genrsa -des3 -out client.key 2048
pass phrase: plclient0618

[root@localhost client]# openssl req -new -key client.key -out client.csr
Enter pass phrase for client.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
—–
Country Name (2 letter code) [GB]:cn
State or Province Name (full name) [Berkshire]:shanghai
Locality Name (eg, city) [Newbury]:shanghai
Organization Name (eg, company) [My Company Ltd]:gongsilong
Organizational Unit Name (eg, section) []:business
Common Name (eg, your name or your server’s hostname) []:ops
Email Address []:mch@gongsilong.com

Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:plclient0618
An optional company name []:gongsilong

4.4 生成的 CSR 证书文件必须有 CA 的签名才可形成证书. 这里制作自己的 CA 这时生成一个 KEY 文件 ca.key 和根证书 ca.crt
pass phrase: gongsilong0617

[root@localhost ssl]# openssl req -new -x509 -nodes -keyout ca.key -out ca.crt
Generating a 1024 bit RSA private key
…….++++++
…………….++++++
writing new private key to ‘ca.key’
Enter PEM pass phrase:
Verifying – Enter PEM pass phrase:
—–
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
—–
Country Name (2 letter code) [GB]:cn
State or Province Name (full name) [Berkshire]:shanghai
Locality Name (eg, city) [Newbury]:
writing new private key to ‘ca.key’Organization Name (eg, company) [My Company Ltd]:
[root@localhost ssl]# openssl req -new -x509 -keyout ca.key -out ca.crt
Generating a 1024 bit RSA private key
…………..++++++
…………………………………………..++++++
writing new private key to ‘ca.key’
Enter PEM pass phrase:
Verifying – Enter PEM pass phrase:
—–
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
—–
Country Name (2 letter code) [GB]:cn
State or Province Name (full name) [Berkshire]:shanghai
Locality Name (eg, city) [Newbury]:shanghai
Organization Name (eg, company) [My Company Ltd]:gongsilong
Organizational Unit Name (eg, section) []:business
Common Name (eg, your name or your server’s hostname) []:ops
Email Address []:mch@gongsilong.com
[root@localhost ssl]#
[root@localhost ssl]# mch@gongsilong.com
-bash: mch@gongsilong.com: command not found
[root@localhost ssl]#

签署证书准备工作:
[root@mail ssl]# vim /etc/pki/tls/openssl.cnf
#dir = ../../CA // 修改如下
dir = /etc/pki/plocc/CA


touch /etc/pki/plocc/CA/{index.txt,serial}
[root@localhost ssl]# ll /etc/pki/plocc/CA/
总计 0
-rw-r–r– 1 root root 0 06-18 10:47 index.txt
-rw-r–r– 1 root root 0 06-18 10:47 serial
[root@localhost ssl]# echo 01 > /etc/pki/plocc/CA/serial
[root@localhost ssl]# mkdir /etc/pki/plocc/CA/newcerts

4.5 用生成的 CA 的证书 (ca.crt) 为刚才生成的 server.csr,client.csr 文件签名
pass phrase:gongsilong0617
openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key

[root@localhost ssl]# openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key
Using configuration from /etc/pki/tls/openssl.cnf
Enter pass phrase for ca.key:
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Jun 18 04:04:09 2014 GMT
Not After : Jun 18 04:04:09 2015 GMT
Subject:
countryName = cn
stateOrProvinceName = shanghai
organizationName = baolong
organizationalUnitName = business
commonName = ops
emailAddress = mch@gongsilong.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
52:6A:D9:56:CB:2B:DA:E3:9A:18:CC:FE:4D:A1:8C:21:86:55:D5:11
X509v3 Authority Key Identifier:
keyid:4E:F5:29:7F:6B:AD:11:EF:FC:44:CC:76:1D:B0:B9:F7:4B:9D:CB:93

Certificate is to be certified until Jun 18 04:04:09 2015 GMT (365 days)
Sign the certificate? [y/n]:y

1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
[root@localhost ssl]#

[root@localhost ssl]# openssl ca -in client.csr -out client.crt -cert ca.crt -keyfile ca.key
Using configuration from /etc/pki/tls/openssl.cnf
Enter pass phrase for ca.key:
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 2 (0x2)
Validity
Not Before: Jun 18 04:10:40 2014 GMT
Not After : Jun 18 04:10:40 2015 GMT
Subject:
countryName = cn
stateOrProvinceName = shanghai
organizationName = baolong
organizationalUnitName = business
commonName = ops
emailAddress = mch@gongsilong.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
E2:64:97:DC:A6:2B:85:53:5F:6C:5C:8D:1F:EB:59:C8:2C:66:C5:10
X509v3 Authority Key Identifier:
keyid:4E:F5:29:7F:6B:AD:11:EF:FC:44:CC:76:1D:B0:B9:F7:4B:9D:CB:93


Certificate is to be certified until Jun 18 04:10:40 2015 GMT (365 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
[root@localhost ssl]#


[PS]:附带功能:
另外,这个 certificate 是 BASE64 形式的, 要转成 PKCS12 才能装到 IE,/NETSCAPE 上. 转换如下:
双击安装就行
openssl pkcs12 -export -clcerts -in client.crt -inkey client.key -out client.p12
这个是 ISO 需要的证书格式
openssl x509 -in client.crt -out client.cer
这个是 Android 需要的证书格式。
[root@mail ssl]# openssl pkcs12 -export -in client.crt -inkey client.key -out client.pfx
Enter pass phrase for client.key: // 客户端私钥密码
Enter Export Password: //pfx 文件导入要求的密码
Verifying – Enter Export Password:

[root@localhost conf]# service nginx stop
stop nginx
Enter PEM pass phrase:
phrase is too short, needs to be at least 4 chars
Enter PEM pass phrase:
phrase is too short, needs to be at least 4 chars
Enter PEM pass phrase:

nginx 启动 SSL 默认不输入密码
如果 nginx 配置了 SSL,在每次启动 nginx 的时候都会需要你手动输入证书的密码,如果不想输入,可以
cp server.key server.key.orig
openssl rsa -in server.key.orig -out server.key
这样启动 nginx 的时候就不需要输入密码了。

[root@localhost ssl]# cp server.key server.key.orig
[root@localhost ssl]# openssl rsa -in server.key.orig -out server.key
Enter pass phrase for server.key.orig:
unable to load Private Key
20487:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:325:
20487:error:0906A065:PEM routines:PEM_do_header:bad decrypt:pem_lib.c:425:
[root@localhost ssl]#

这里奇怪,一开始通不过,但是过了 15 分钟后,在运行一遍,输入密码,又通过了,如下所示:
[root@localhost ssl]# openssl rsa -in server.key.orig -out server.key
Enter pass phrase for server.key.orig:
writing RSA key
[root@localhost ssl]#

当然也可以保留密码,改用 expect 的方式,这个可以参考 expect 自动登录 SSH 的方法,下次有时间再整理贴上来

5 静态文件地址映射 nginx
location = userWeb/userCenter/findConsultList.htm {
rewrite ^.*$ http://xx.gongsilong.com/xx/xx/findConsultList.htm;
}


# add by tim begin …
location ~* ^/svn/(.*) {
rewrite ^.*$ https://192.123.11.12/$1;
}
# add by tim end ..

Nginx 的详细介绍 :请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-07/104499.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计19225字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

星哥玩云

星哥玩云
星哥玩云
分享互联网知识
用户数
4
文章数
19350
评论数
4
阅读量
7960669
文章搜索
热门文章
星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛 NAS-6:抖音视频同步工具,视频下载自动下载保存 前言 各位玩 NAS 的朋友好,我是星哥!...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见 zabbix!轻量级自建服务器监控神器在 Linux 的完整部署指南 在日常运维中,服务器监控是绕不开的...
飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛 NAS 中安装 Navidrome 音乐文件中文标签乱码问题解决、安装 FntermX 终端 问题背景 ...
阿里云CDN
阿里云CDN-提高用户访问的响应速度和成功率
随机文章
Prometheus:监控系统的部署与指标收集

Prometheus:监控系统的部署与指标收集

Prometheus:监控系统的部署与指标收集 在云原生体系中,Prometheus 已成为最主流的监控与报警...
【1024程序员】我劝你赶紧去免费领一个AWS、华为云等的主机

【1024程序员】我劝你赶紧去免费领一个AWS、华为云等的主机

【1024 程序员】我劝你赶紧去免费领一个 AWS、华为云等的主机 每年 10 月 24 日,程序员们都会迎来...
星哥带你玩飞牛NAS-11:咪咕视频订阅部署全攻略

星哥带你玩飞牛NAS-11:咪咕视频订阅部署全攻略

星哥带你玩飞牛 NAS-11:咪咕视频订阅部署全攻略 前言 在家庭影音系统里,NAS 不仅是存储中心,更是内容...
小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比

小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比

小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比 星哥玩云,带你从小白到上云高手。今天咱们就来聊聊——什...
星哥带你玩飞牛NAS硬件03:五盘位+N5105+双网口的成品NAS值得入手吗

星哥带你玩飞牛NAS硬件03:五盘位+N5105+双网口的成品NAS值得入手吗

星哥带你玩飞牛 NAS 硬件 03:五盘位 +N5105+ 双网口的成品 NAS 值得入手吗 前言 大家好,我...

免费图片视频管理工具让灵感库告别混乱

一言一句话
-「
手气不错
三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Android 的最优解?

三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Android 的最优解?

  三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Andr...
星哥带你玩飞牛 NAS-9:全能网盘搜索工具 13 种云盘一键搞定!

星哥带你玩飞牛 NAS-9:全能网盘搜索工具 13 种云盘一键搞定!

星哥带你玩飞牛 NAS-9:全能网盘搜索工具 13 种云盘一键搞定! 前言 作为 NAS 玩家,你是否总被这些...
星哥带你玩飞牛NAS-16:飞牛云NAS换桌面,fndesk图标管理神器上线!

星哥带你玩飞牛NAS-16:飞牛云NAS换桌面,fndesk图标管理神器上线!

  星哥带你玩飞牛 NAS-16:飞牛云 NAS 换桌面,fndesk 图标管理神器上线! 引言 哈...
每天一个好玩的网站-手机博物馆-CHAZ 3D Experience

每天一个好玩的网站-手机博物馆-CHAZ 3D Experience

每天一个好玩的网站 - 手机博物馆 -CHAZ 3D Experience 一句话介绍:一个用 3D 方式重温...
还在找免费服务器?无广告免费主机,新手也能轻松上手!

还在找免费服务器?无广告免费主机,新手也能轻松上手!

还在找免费服务器?无广告免费主机,新手也能轻松上手! 前言 对于个人开发者、建站新手或是想搭建测试站点的从业者...