Nginx缺少http_ssl_module与Nginx重定向


Nginx 缺少 http_ssl_module 与 Nginx 重定向

前言

承接 Nginx SSL证书部署 中使用 Nginx 时会遇见的几个问题,本篇文章展开介绍 Nginx 缺少 http_ssl_module 与 Nginx 重定向的内容。

Nginx 缺少 http_ssl_module

错误信息

前文中若是重启 nginx 后控制台报错:the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:171

这是因为编译安装 nginx 的时候缺少 http_ssl_module 模块,需要重新下载编译。

解决步骤

#下载https模块
cd /usr/local/nginx-1.16.1/
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
#重新编译
make
#查询nginx进程
ps -elf | grep nginx
#杀掉进程
kill -9 pid
#使用objs/nginx覆盖原有的配置
cp ./objs/nginx /usr/local/nginx/sbin/
#重新启动Nginx
cd /usr/local/nginx/sbin/
./nginx
  • 使用 https 协议访问下配置的域名测试一下

Nginx 重定向

如果想同时允许 http 与 https 协议访问网站,或者旧地址舍弃如何切换到另外的地址访问呢?

Nginx 中可以通过 rewrite 指令进行重定向操作,示例如下:

# 重定向为https协议访问
return 301 https://$http_host$request_uri;
# 重定向为http协议访问
return 301 http://$http_host$request_uri;

Nginx 配置分享

server {
        listen       80;
        server_name  yourweb;
        proxy_connect_timeout 120;
        proxy_read_timeout    300;
        proxy_send_timeout    300;
        send_timeout          120;
        # 重定向
        return 301 https://$http_host$request_uri;
    }

server {
        proxy_connect_timeout 500;
        proxy_read_timeout    500;
        proxy_send_timeout    500;
        send_timeout          500;

        listen       443 ssl;
        server_name  yourweb;
        ssl_certificate      /usr/local/nginx/cert/yourweb.com.pem;
        ssl_certificate_key  /usr/local/nginx/cert/yourweb.com.key;

        ssl_session_cache    shared:SSL:5m;
        ssl_session_timeout  5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
        #ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

		location / {
		    root /usr/local/test;# 你的文件目录
		    index index.html;
		    try_files $uri $uri/ $uri.html ;
         }     
	        error_page   403 404  /40x.html;
	        location = /40x.html {
	            root   /usr/local/public;
         }
    }

结语

历经以上种种困难,终于搭建出自己的数字花园,浏览器上访问域名后类似如下图,这一刻成就感满满。可以骄傲一会,然而更多的坑还在后面等着我们去填平……

如果你感兴趣,可以访问 Huan’s Garden 或者 Huan’s Blog,或许我的数字花园与博客中藏有你感兴趣的内容……


文章作者: huan
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 huan !
  目录