前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx之Windows下Nginx下带有https的图片路径搭建

Nginx之Windows下Nginx下带有https的图片路径搭建

作者头像
周杰伦本人
发布2023-10-12 14:40:38
4310
发布2023-10-12 14:40:38
举报
文章被收录于专栏:同步文章同步文章

Windows下Nginx下带有https的图片路径搭建

今天玩个高端的 由于生产环境的图片地址 https://www.cginx.com/images/20190423094936_885186.jpg 是这个 我在本地环境的项目无法显示图片 于是想着在windows环境下搭建个Nginx 来显示图片

1.Windows实现nginx作为图片服务器

关键配置:

代码语言:javascript
复制
	#浏览器打开路径:localhost:8089(listen端口)/location设置的路径/图片名字.后缀
   	location ~ /images/.*\.(gif|jpg|jpeg|png)$ {
   	#图片保存在电脑的地址
   	root  D:/files/new/cityZone;
   	}

location 表示访问路径 root 表示 ~代表的实际路径 我是这么认为的。。。

参考博客 https://blog.csdn.net/weixin_39220472/article/details/80330972

然后重新加载配置文件就ojbk了 然而发现都是默认http开头的 就想着怎么把http变成https

2.http变成https

具体步骤: 1.安装Openssl http://slproweb.com/products/Win32OpenSSL.html 2.安装ActivePerl (此软件目的为了解析pl文件) 3. 配置环境变量

在环境变量中添加环境变量     变量名: OPENSSL_HOME     变量值:C:\wnmp\OpenSSL-Win64\bin; (变量值为openssl安装位置)     在path变量结尾添加如下 : %OPENSSL_HOME%; 4. 生成证书

(1) 首先在 nginx安装目录中创建ssl文件夹用于存放证书。比如我的文件目录为 C:\wnmp\nginx\ssl

以管理员身份进入命令行模式,进入ssl文件夹。 命令为: cd c:/wnmp/nginx/ssl

(2) 创建私钥

在命令行中执行命令: openssl genrsa -des3 -out lee.key 1024(lee文件名可以自定义), 输入密码后,再次重复输入确认密码。记住此密码,后面会用到。

(3)创建csr证书

在命令行中执行命令: openssl req -new -key lee.key -out lee.csr(key文件为刚才生成的文件,lee为自定义文件名)      执行上述命令后,需要输入信息。输入的信息中最重要的为 Common Name,这里输入的域名即为我们要使用https访问的域名。我这里输入 www.cginx.com (4)去除密码。

在加载SSL支持的Nginx并使用上述私钥时除去必须的口令,否则会在启动nginx的时候需要输入密码。

复制lee.key并重命名为lee.key.org

可以使用此命令行,也可以使用鼠标操作 copy lee.key lee.key.org

去除口令,在命令行中执行此命令: openssl rsa -in lee.key.org -out lee.key(lee为自定义文件名)

如下图所示,此命令需要输入刚才设置的密码。 (5)生成crt证书

在命令行中执行此命令: openssl x509 -req -days 365 -in lee.csr -signkey lee.key -out lee.crt(lee为自定义文件名) 证书生成完毕,ssl文件夹中一共生成如下4个文件,我们需要使用到的是lee.crt和lee.key。

在这里插入图片描述
在这里插入图片描述

5. 修改nginx.conf文件 主要是listen 443 ssl; 这一段位置的配置文件 原来的注释掉了 我们放开注释 然后改改

这里贴出我的nginx.conf完整配置文件

代码语言:javascript
复制
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #浏览器打开路径:localhost:8089(listen端口)/location设置的路径/图片名字.后缀
		location ~ /images/.*\.(gif|jpg|jpeg|png)$ {
		#图片保存在电脑的地址
		root  D:/files/new/cityZone;
		}
        #location /images/ {
        #     alias D:/images/;
        #     autoindex on;
        #}


        #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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    server {
        listen       443 ssl;
        server_name    www.cginx.com;
    
        ssl_certificate      D://soft//nginx-1.8.0//ssl//lee.crt;
        ssl_certificate_key  D://soft//nginx-1.8.0//ssl//lee.key;
    
        #ssl_session_cache    shared:SSL:1m;
        #ssl_session_timeout  5m;
    
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
    
        location / {
            root   html;
            index  index.html index.htm index.php;
        }
		location ~ /images/.*\.(gif|jpg|jpeg|png)$ {
		#图片保存在电脑的地址
		root  D:/files/new/cityZone;
		}

    }
    
}

这里注意配置文件中秘钥的路径的写法。。反正我是这么写的 其他写法好不好使我也母鸡

还有就是把hosts文件改一下: 127.0.0.1 www.cginx.com hosts文件的本质就是屏蔽掉一些网站 没有改hosts之前www.cginx.com是可以访问的 改了hosts后www.cginx.com就不能访问到了 然后我们启动Nginx就可以访问磁盘中的图片了

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

遇到的坑: 2019/04/25 11:24:24 [emerg] 10020#14912: shared zone “SSL” has no equal addresses: 031A0000 vs 03240000

原因好像是windows版本的Nginx不支持缓存 解决办法就是我把配置文件中的ssl_session_cache shared:SSL:1m;缓存注释掉。。

完美 perfect

一位老师说的话:有的东西有些项目我们由于各种原因接触不到 但是我们把他人的东西研究透彻了 知识也就是自己的了 我们也可以为自己所用

参考博客: https://www.cnblogs.com/chasewade/p/7661290.html

https://www.cnblogs.com/vincent-li666/p/5851463.html

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-10-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Windows下Nginx下带有https的图片路径搭建
    • 1.Windows实现nginx作为图片服务器
      • 2.http变成https
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档