电磁炮战列舰 發表於 2020-7-28 10:17:00

linux 搭建nginx图片服务器 实现预览

<h2><strong>首先</strong><strong>安装nginx安装环境</strong></h2>
<p>nginx是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境。</p>
<ul>
<li class="15">&nbsp;gcc</li>
</ul>
<p>安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:</p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre>yum install gcc-c++</pre>
</div>
<p>&nbsp;</p>
<ul>
<li class="15">&nbsp;PCRE</li>
</ul>
<p>PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括&nbsp;perl&nbsp;兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。</p>
<div class="cnblogs_code">
<pre>yum install -y pcre pcre-devel</pre>
</div>
<p>注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。</p>
<ul>
<li class="15">&nbsp;zlib</li>
</ul>
<p>zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。</p>
<div class="cnblogs_code">
<pre>yum install -y zlib zlib-devel</pre>
</div>
<ul>
<li class="15">&nbsp;openssl</li>
</ul>
<p>OpenSSL&nbsp;是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。</p>
<p>nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。</p>
<div class="cnblogs_code">
<pre>yum install -y openssl openssl-devel</pre>
</div>
<h2><strong>然后安装</strong><strong>nginx</strong></h2>
<p>到Nginx官网下载tar.gz格式的安装包,这里下载的是nginx-1.10.3版本,环境使用centos的虚拟机</p>
<p>1、将安装包上传,解压,命令<strong>tar -</strong><strong>z</strong><strong>xvf nginx-1.10.3.tar.gz</strong></p>
<p>2、自定义创建一个文件夹作为Nginx安装目录,这里在home下<strong>创建nginx文件夹</strong>;</p>
<p><img src="https://img2020.cnblogs.com/blog/2091912/202007/2091912-20200728101318001-757556680.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>3、在解压的文件夹(nginx-1.10.3)下执行<strong>./configure --prefix=/home/nginx</strong>&nbsp;命令。&nbsp;<br>意思即配置安装环境,将会把Nginx安装到/home/nginx下;</p>
<p>4、编译:在解压的文件夹下先后执行<strong>make&nbsp;和&nbsp;make install&nbsp;</strong>命令</p>
<p>执行<strong>make</strong></p>
<p><strong><img src="https://img2020.cnblogs.com/blog/2091912/202007/2091912-20200728101335671-973131684.png" alt="" loading="lazy"></strong></p>
<p>&nbsp;</p>
<p>&nbsp;然后执行<strong>make install</strong></p>
<p><img src="https://img2020.cnblogs.com/blog/2091912/202007/2091912-20200728101351732-782511793.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>5、Nginx默认使用端口是80,这里直接先把Nginx端口改为8088,</p>
<p><strong>vi /home/nginx/conf/nginx.conf</strong>,修改server的端口,并配置一个图片服务器</p>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;">userroot;
worker_processes1;

error_loglogs/error.log debug;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;

#pid      logs/nginx.pid;


events {
    worker_connections1024;
}


http {
    include       mime.types;
    default_typeapplication/octet-stream;

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

    #access_loglogs/access.logmain;

    sendfile      on;
    #tcp_nopush   on;

    #keepalive_timeout0;
    keepalive_timeout65;

    #gzipon;

    server {
      listen       888;
      server_namelocalhost;

      #charset koi8-r;

      #access_loglogs/host.access.logmain;

       
        location ~ .*\.(gif|jpg|jpeg|png)$ {
            expires 24h;
            root /home/www/FTP/;
            access_log /home/nginx/logs/images.log;
            proxy_store on;
            proxy_store_access user:rw group:rw all:rw;
            proxy_temp_path         /home/www/FTP/;
            proxy_redirect          off;
            proxy_set_header      Host 127.0.0.1;
            client_max_body_size    10m;
            client_body_buffer_size 1280k;
            proxy_connect_timeout   900;
            proxy_send_timeout      900;
            proxy_read_timeout      900;
            proxy_buffer_size       40k;
            proxy_buffers         40 320k;
            proxy_busy_buffers_size 640k;
            proxy_temp_file_write_size 640k;
         
      }

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

      #error_page404            /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_indexindex.php;
      #    fastcgi_paramSCRIPT_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 {
      #    denyall;
      #}
    }


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

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


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_namelocalhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_keycert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout5m;

    #    ssl_ciphersHIGH:!aNULL:!MD5;
    #    ssl_prefer_server_cipherson;

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

}
</pre>
</div>
<p>  </p>
<p>server下listen的端口改为8088,并增加一个location配置,用于访问图片文件,这一串配置有#号在前面的注释掉的都可以删掉,免得看起来又长又乱。</p>
<p>6、启动命令:&nbsp;<strong>/home/nginx/sbin/nginx -c /home/nginx/conf/nginx.conf</strong><br>打开防火墙对应端口供访问,8088,当然也可以直接关了防火墙。&nbsp;<br><strong>若修改了nginx.conf配置,则需要重启才生效,命令:/home/nginx/sbin/nginx -s reload</strong></p>

</div>
<div id="MySignature" role="contentinfo">
    码畜世界,码畜天地,码畜星辰<br><br>
来源:https://www.cnblogs.com/mnwd/p/13389561.html
頁: [1]
查看完整版本: linux 搭建nginx图片服务器 实现预览