美文网首页
nginx - https

nginx - https

作者: xiongzenghui | 来源:发表于2018-08-12 13:12 被阅读49次

1、http 重定向

[图片上传失败...(image-ca2188-1534050760876)]

2、一台主机,同时提供http与https两种访问方式

1. nginx.conf

  • 1)域名站点最终通过https访问
  • 2)http首先访问nginx后,重定向到https访问(http => nginx => https)
http
{
  # 1、https 443端口:https://www.domain.com
  server
  {
    listen 443;
    ssl on;
    server_name domain.com; //你的域名
    index index.html index.htm index.php default.html default.htm default.php;
    ssl_certificate /usr/local/nginx/cert/user.medsci-tech.com/214020580630662.pem;
    ssl_certificate_key /usr/local/nginx/cert/user.medsci-tech.com/214020580630662.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    root /home/wwwroot/web/public;//项目根目录
    include laravel.conf;
    #error_page 404 /404.html;
    include enable-php.conf;
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
      expires 30d;
    }
    location ~ .*\.(js|css)?$
    {
      expires 12h;
    }
  }

  # 2、http 80端口:http://www.domain.com, 重定向到 https 443端口
  server 
  {
    listen 80;
    server_name domain.com;

    ## 重定向到 443 https 方式访问
    rewrite ^/(.*) https://$server_name$request_uri? permanent;
  }
}
  • 1)nginx服务部署所在主机,直接作为【http服务器】
  • 2)同时提供【80端口】与【443】端口
  • 3)http重定向为https
  • 4)如果是【自制ssl证书】,则需要在【app客户端】预先存放【ssl公钥证书】
  • 5)如果是【权威认证机构颁发的ssl证书】,则不需要做什么处理

1. 直接使用 http

[图片上传失败...(image-199c6e-1534050760876)]

2. http 转发为 https

[图片上传失败...(image-287921-1534050760876)]

3、【nginx 转发服务】与【nginx http服务】分离

1. 逻辑流程

[图片上传失败...(image-f5f4f5-1534050760876)]

2. nginx 服务器

nginx 服务器类型 服务器编号
nginx 转发服务器 A 8080
nginx http web 服务器 B

3.【A主机】nginx.conf

server 
{
  listen       8080;
  server_name  domain.com;
  
  # 1、将对localhost访问由/opt/app/lua/dep.lua进行处理
  location /
  {
    default_type "text/html"; 
    content_by_lua_file /opt/app/lua/dep.lua; # 指定由lua文件处理http请求
    #add_after_body "$http_x_forwarded_for";
  }

  ############################################################
  # 2.1、http 协议进行转发
  location @go_http
  {
    # 访问nginx【http】服务器的http协议时,
    # => 使用 proxy_pass 转发
    # => proxy_pass【不会让地址栏发生变化】
    proxy_pass http://【nginx http服务器ip】:80; 
  }

  ############################################################
  # 2.2、https 协议转发进行转发
  location @go_https
  {
    # 访问nginx【https】服务器的http协议时
    # => 使用 rewrite 重定向
    # => rewrite 【让地址栏发生变化,重新发起https连接访问】
    # => 如果是【自制ssl证书】,则需要在【app客户端】预先存放【ssl公钥证书】
    # => 如果是【权威认证机构颁发的ssl证书】,则不需要做什么处理
    rewrite ^/(.*) https://$server_name$request_uri? permanent;
  }

  error_page   500 502 503 504 404  /50x.html;
  location = /50x.html {
    root   /usr/share/nginx/html;
  }
}

4.【A主机】/opt/app/lua/dep.lua

1. lua 源文件

-- 查询memcache等数据库服务,
-- 判断ip是否包含在http或https协议访问配置中

-- 走http协议
if res == "1" then
  ngx.exec("@go_http")
  return
end

-- 走htts协议
ngx.exec("@go_https")

2. ngx.exec("@go_https")

location @go_https
{
  rewrite ^/(.*) https://$server_name$request_uri? permanent;
}

3. ngx.exec("@go_http")

location @go_http
{
  proxy_pass http://【nginx http服务器ip】:80; 
}

会自动重定向为上面的https链接访问。

5.【B主机】nginx.conf

http
{
  # 1. 443端口 https://www.domain.com
  server
  {
    listen 443;
    ssl on;
    server_name localhost; //你的域名
    index index.html index.htm index.php default.html default.htm default.php;
    ssl_certificate /usr/local/nginx/cert/user.medsci-tech.com/214020580630662.pem;
    ssl_certificate_key /usr/local/nginx/cert/user.medsci-tech.com/214020580630662.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    root /home/wwwroot/web/public;//项目根目录
  }

  # 2. 80端口 https://www.domain.com
  server 
  {
    listen 80;
    server_name localhost;

    root /home/wwwroot/web/public;//项目根目录
  }
}
  • 1)提供【80端口】的http请求处理
  • 2)也提供【443端口】的http请求处理

6. rewrite 与 proxy_pass

rewrite 重定向 让客户端重新发起连接,访问新的url地址,地址栏会发生变化
proxy_pass 代理转发 直接将客户端请求塞给另外服务器处理,地址栏【不发生】变化

4、nginx 497 响应码

1. 核心

  • 1、当网站只允许https访问时,当用http访问时nginx会报出497错误码
  • 2、利用error_page命令,将497状态码的链接重定向到https域名

2. nginx.conf

server 
{
  listen       127.0.0.1:443;  #ssl端口 
  listen       127.0.0.1:80;   #用户习惯用http访问,加上80,后面通过497状态码让它自动跳到443端口 
  server_name  dev.wangshibo.com;

  #为一个server{......}开启ssl支持 
  ssl                  on; 
  
  #指定PEM格式的证书文件  
  ssl_certificate      /etc/nginx/wangshibo.pem;  
  
  #指定PEM格式的私钥文件 
  ssl_certificate_key  /etc/nginx/wangshibo.key; 
      
  #让http请求重定向到https请求  
  error_page 497  https://$host$uri?$args; 

  location ~ / {
  root /var/www/html/8080;
  index index.html index.php index.htm;
  }
}

5、proxy_redirec

## re-write redirects to http as to https, example: /home
proxy_redirect http:// https://;

相关文章

网友评论

      本文标题:nginx - https

      本文链接:https://www.haomeiwen.com/subject/jijsbftx.html