说明

一级域名类似于baidu.com这样的,像www.biadu.com、tieba.baidu.com这样的属于二级域名,我们平时买的都是一级域名,有了一级域名之后对于二级域名我们是可以根据自己的需要随意配置的,我们的目的是配置出http(s)://www.xxxx.cn和http(s)://blog.xxxx.cn这样的二级可以用http(s)访问的域名。

并且是在一台服务器的Nginx下配置多个二级域名(例如:www.xxxx.cn和blog.xxxx.cn)及其CA证书(HTTPS)(emm 没办法,穷,一台服务器得多用)

快速开始

先来熟悉一下nginx的基本命令(如果nginx是默认安装的话,nginx在/usr/local/nginx下):

/usr/local/nginx/sbin/nginx  启动/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  启动,载入当前配置/usr/local/nginx/sbin/nginx -t 测试配置/usr/local/nginx/sbin/nginx -s reload   加载配置--不是重启,但可以当重启使用/usr/local/nginx/sbin/nginx -s stop    退出/usr/local/nginx/sbin/nginx -s quit  保持未结束的进程后退出

好了,进入正题,既然要配置HTTPS,首先得有CA证书,这里以腾讯云的CA证书和域名为例(注意,由于现在免费的CA证书不是通配符的,所以我们必须为www.xxxx.cn和blog.xxxx.cn都得申请证书)。

申请CA证书戳-->https://console.cloud.tencent.com/ssl,在这按照提示申请CA证书

然后添加域名解析戳->https://console.cloud.tencent.com/domain/mydomain,添加www和blog的主机记录,记录值填你的服务器IP最后类似下图

然后下载你的两个CA证书,解压选择nginx目录下的两个文件(可以对其重命名,否则太长可能出问题,例如1_www.crt和1_www.key,1_blog.ctr和1_blog.key),将这四个文件拷贝到你服务器的/usr/local/nginx/conf/目录下。

然后修改nginx.conf文件:

 # 配置访问www.xxxx.cn的请求server {listen 80 default_server;listen 443 ssl;charset utf-8;server_name  www.xxxx.cn;ssl_certificate /usr/local/nginx/conf/1_www.crt;ssl_certificate_key /usr/local/nginx/conf/1_www.key;ssl_session_timeout 5m;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;ssl_prefer_server_ciphers on;#location ~ \.jsp$ {# root /root/tomcat-8.5/webapps;#     proxy_pass http://127.0.0.1:8080;#   proxy_set_header Host $host:       $server_port;#   proxy_set_header X-Real-IP             $remote_addr;#   proxy_set_header X-Real-PORT       $remote_port;#   proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;#}#location ~ \.html$ {# root /root/tomcat-8.5/webapps;#     proxy_pass http://127.0.0.1:8080;#  proxy_set_header Host               $http_addr;#    proxy_set_header X-Real-IP          $remote_addr;#  proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;#}location ~ /^[H,h][T,t][T,t][P,p][S,s]/ {proxy_pass http://127.0.0.1:8080;proxy_set_header Host                $http_addr;proxy_set_header X-Real-IP           $remote_addr;proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;#以上三行,目的是将代理服务器收到的用户的信息传到真实服务器上root /root/tomcat-8.5/webapps;index index.html index.htm;            #默认情况}location / {proxy_pass http://127.0.0.1:8080;proxy_set_header Host              $http_addr;proxy_set_header X-Real-IP           $remote_addr;proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;root /root/tomcat-8.5/webapps;index index.html index.htm;            #默认情况}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}}# 配置访问blog.xxxx.cn的请求server {listen 80;listen 443 ssl;charset utf-8;server_name  blog.xxxx.cn;ssl_certificate /usr/local/nginx/conf/1_blog.crt;ssl_certificate_key /usr/local/nginx/conf/1_blog.key;ssl_session_timeout 5m;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;ssl_prefer_server_ciphers on;location ~ /^[H,h][T,t][T,t][P,p][S,s]/ {proxy_pass http://127.0.0.1:8081;proxy_set_header Host                 $http_addr;proxy_set_header X-Real-IP           $remote_addr;proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;#以上三行,目的是将代理服务器收到的用户的信息传到真实服务器上root /root/tomcat-8.5/webapps;index index.html index.htm;            #默认情况}# 这里配置使用HTTP访问的请求location / {proxy_pass http://127.0.0.1:8081;proxy_set_header Host                 $http_addr;proxy_set_header X-Real-IP           $remote_addr;proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;root /root/tomcat-8.5/webapps;index index.html index.htm;            #默认情况}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}

这样就成功将访问http(s)://www.xxxx.cn的转发到8080端口,而访问http(s)://blog.xxxx.cn的转发到8081端口

最后测试看nginx配置是否正确:

/usr/local/nginx/sbin/nginx -t 测试配置/usr/local/nginx/sbin/nginx -s reload   加载配置--不是重启,但可以当重启使用

Nginx配置多个二级域名和多个CA证书相关推荐

  1. nginx配置一、二级域名、多域名对应(api接口、前端网站、后台管理网站)

    前提:安装好nginx,如果已经启动nginx,先停止,命令: ./usr/local/nginx/sbin/nginx -s stop 修改nginx配置 vi /usr/local/nginx/c ...

  2. 蚂蚁分类信息系统Nginx伪静态规则支持二级域名设置方法

    蚂蚁分类信息系统Nginx伪静态规则支持二级域名设置方法 nginx环境中蚂蚁分类信息系统支持二级域名访问实现方法 1.后台生成所有城市目录 2.nginx伪静态增加配置 if ( $host ~* ...

  3. Nginx 配置Godaddy下载的没有.key 文件的SSL证书

    Nginx 配置Godaddy下载的没有.key 文件的SSL证书 一.为Godaddy的ssl配置.csr文件 1. 如果是新购买ssl证书:1. 控制台点击"SSL CERTIFICAT ...

  4. 阿里云添加二级域名及生成免费ssl证书

    阿里云添加二级域名及生成免费ssl证书 一.问题背景 app上架需要绑定域名进行解析,需要提供域名及对应的ssl证书 二.解决方案 1.公司已有域名,生成二级域名和免费ssl证书提供 三.具体操作 1 ...

  5. Nginx反向代理 设置二级域名 (CentOS)

    nginx 配置 whereis nginx /usr/sbin/nginx     ->    执行文件 /etc/nginx/conf.d  ->    自定义.conf文件的存放位置 ...

  6. nginx配置多个一级域名https访问的配置

    首先已有两个域名 www.test1.com ,www.test2.com nginx的安装略过 1.在nginx安装目录下创建cert目录 在Nginx的安装目录下创建cert目录 2.在cert目 ...

  7. Nginx配置WS、WSS域名

    文章目录 一.Nginx配置WS 二.Nginx配置WSS 一.Nginx配置WS WS的全称是WebSocket,Nginx配置WebSocket也比较简单,只需要在nginx.conf文件中进行相 ...

  8. nginx配置-根据UA进行域名跳转且仅限域名访问

    随着移动用户的增长,web应用中,根据用户UA进行PC端.移动端的跳转是必要的,常见的方式有三种:前端js根据UA做重定向,后端根据UA返回不同的路径,在nginx中处理,这里用的是第三种.还配置了 ...

  9. nginx配置重启后新域名及其访问不起作用

    1.nginx配置2个server后 第一个域名1,访问第一个ip1:port1 第二个域名2,访问第二个ip1:port2 2.重启nginx后,使用第一个域名访问正常.使用第二个域名访问时,访问的 ...

  10. Windows 2008 R2 配置 DNS 实现二级域名

    本文内容 • 域名解析 • 准备工作 • 安装 DNS 服务器 • 建立 DNS 区域 • 建立主机头 • 服务器网络设置 • 测试二级域名 • IIS 建立 Web 站点 • 其他 DNS 服务 域 ...

最新文章

  1. Oracle Listener 动态注册 与 静态注册
  2. linux下的struct sigaction
  3. java排序两个数组_java – 如何相对于彼此排序两个数组.
  4. 笔记本电脑键盘切换_真想本小新13pro搭档,笔记本电脑周边好物清单推荐
  5. Java怎么避免重复订单_javaEE高并发之如何产生唯一不重复订单号
  6. Netrunner 2019.04 Rolling 版本发布
  7. 03-03 APP 控件定位
  8. 腾讯正式入局中视频领域
  9. Oracle密码过期ORA-28001
  10. 图片-标签、格式\内联框架\音视频播放——HTML
  11. BZOJ 2434 阿狸的打字机(ac自动机+dfs序+树状数组)
  12. Cookie顶级域名、二级域名、三级域名共享
  13. python qq 聊天记录词云制作
  14. 无人值守地磅称重系统方案的设计原理
  15. Launcher壁纸来源
  16. 课堂笔记(3) 假设检验 Hypothesis testing
  17. 【uni-app】什么是uni-app?如何进行开发?如何连接微信开发者工具与安卓手机端?
  18. 机械臂速成小指南(零):指南主要内容及分析方法
  19. redis分片式集群
  20. 双非研二师弟的春招总结和实习感悟

热门文章

  1. maplibre显示经纬线,(动态若干条)
  2. 从研发效能的视角谈“故障复盘”
  3. 程序员培训班要多少米?报名很贵吗?
  4. 用思维导图描绘5G场景
  5. vscode配置代理
  6. 四个步骤告诉你如何进行渠道效果监测
  7. DES加密解密-CryptoJS与Java
  8. 除了技校 哪里还可以学计算机技术,我打算去读技校,技校毕业了去工作有钱了,还可以读技校吗?(我想多学一门技术)...
  9. 联想微型计算机改win7,联想win10改win7如何实现?联想电脑Win10改Win7方法详解
  10. JAVA_OPTS参数说明与配置