(转)Nginx图片服务器

本文转至博客http://wenxin2009.iteye.com/blog/2117079

Nginx搭建图片服务器

Nginx下载地址:http://nginx.org/en/download.html 

本例下载的是window版本nginx-1.6.1

以下是我本机操作说明:

下载完后,解压,并把它放到D: ools ginx-1.6.1,双击nginx.exe即可运行nginx。可通http://127.0.0.1访问到nginx欢迎界面,如下



 也可在cmd中通过命令进行启停启动nginx:

start nginx                      //运行nginx

 nginx -s stop          // 停止nginx

nginx -s reload       // 重新加载配置文件(如修改配置文件后,可通过该命令重新加载)

nginx -s quit          // 退出nginx

nginx -v                 //可查nginx版本

在执行nginx命令时,出现了 windows nginx: [error] CreateFile() "logs/nginx.pid" failed 异常。原因是未指定

nginx.conf,指定该文件,启动命令如下:

D: ools ginx-1.6.1>nginx -c D: ools ginx-1.6.1conf ginx.conf


 

接下来我们配置图片服务器:

1、在本地建了一个D: esourcesfileimages文件夹,里面放了一张png测试图片。

2、配置nginx.conf文件,配置文件内容如下:

Xml代码  收藏代码
  1. #user  nobody;  
  2. worker_processes  1;  
  3.   
  4. #error_log  logs/error.log;  
  5. #error_log  logs/error.log  notice;  
  6. #error_log  logs/error.log  info;  
  7.   
  8. #pid        logs/nginx.pid;  
  9.   
  10.   
  11. events {  
  12.     worker_connections  1024;  
  13. }  
  14.   
  15.   
  16. http {  
  17.     include       mime.types;  
  18.     default_type  application/octet-stream;  
  19.   
  20.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  21.                       '$status $body_bytes_sent "$http_referer" '  
  22.                       '"$http_user_agent" "$http_x_forwarded_for"';  
  23.   
  24.     access_log  logs/access.log  main;  
  25.   
  26.     sendfile        on;  
  27.     #tcp_nopush     on;  
  28.   
  29.     #keepalive_timeout  0;  
  30.     keepalive_timeout  65;  
  31.   
  32.     #gzip  on;  
  33.   
  34.   
  35.     server {  
  36.         listen       8089;#端口号  
  37.         server_name  localhost;#本机  
  38.   
  39.         charset utf-8;  
  40.   
  41.         #access_log  logs/host.access.log  main;  
  42.   
  43.     location ~ .*.(gif|jpg|jpeg|png)$ {  
  44.         expires 24h;  
  45.             root D:/resourcesfile/images/;#指定图片存放路径  
  46.             access_log D:/tools/nginx-1.6.1/logs/log_test.log;#图片路径  
  47.             proxy_store on;  
  48.             proxy_store_access user:rw group:rw all:rw;  
  49.             proxy_temp_path         D:/resourcesfile/images/;#图片路径  
  50.             proxy_redirect          off;  
  51.   
  52.             proxy_set_header        Host 127.0.0.1;  
  53.             proxy_set_header        X-Real-IP $remote_addr;  
  54.             proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;  
  55.             client_max_body_size    10m;  
  56.             client_body_buffer_size 1280k;  
  57.             proxy_connect_timeout   900;  
  58.             proxy_send_timeout      900;  
  59.             proxy_read_timeout      900;  
  60.             proxy_buffer_size       40k;  
  61.             proxy_buffers           40 320k;  
  62.             proxy_busy_buffers_size 640k;  
  63.             proxy_temp_file_write_size 640k;  
  64.             if ( !-e $request_filename)  
  65.             {  
  66.                  proxy_pass  http://127.0.0.1:8089;#代理访问地址  
  67.             }  
  68.     }    
  69.   
  70.         location / {  
  71.             root   html;  
  72.             index  index.html index.htm;  
  73.   
  74.         }  
  75.   
  76.         error_page  404              /404.html;  
  77.   
  78.         # redirect server error pages to the static page /50x.html  
  79.         #  
  80.         error_page   500 502 503 504  /50x.html;  
  81.         location = /50x.html {  
  82.             root   html;  
  83.         }  
  84.   
  85.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  86.         #  
  87.         #location ~ .php$ {  
  88.         #    proxy_pass   http://127.0.0.1;  
  89.         #}  
  90.   
  91.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  92.         #  
  93.         #location ~ .php$ {  
  94.         #    root           html;  
  95.         #    fastcgi_pass   127.0.0.1:9000;  
  96.         #    fastcgi_index  index.php;  
  97.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  98.         #    include        fastcgi_params;  
  99.         #}  
  100.   
  101.         # deny access to .htaccess files, if Apache's document root  
  102.         # concurs with nginx's one  
  103.         #  
  104.         #location ~ /.ht {  
  105.         #    deny  all;  
  106.         #}  
  107.     }  
  108.   
  109.   
  110.     # another virtual host using mix of IP-, name-, and port-based configuration  
  111.     #  
  112.     #server {  
  113.     #    listen       8000;  
  114.     #    listen       somename:8080;  
  115.     #    server_name  somename  alias  another.alias;  
  116.   
  117.     #    location / {  
  118.     #        root   html;  
  119.     #        index  index.html index.htm;  
  120.     #    }  
  121.     #}  
  122.   
  123.   
  124.     # HTTPS server  
  125.     #  
  126.     #server {  
  127.     #    listen       443 ssl;  
  128.     #    server_name  localhost;  
  129.   
  130.     #    ssl_certificate      cert.pem;  
  131.     #    ssl_certificate_key  cert.key;  
  132.   
  133.     #    ssl_session_cache    shared:SSL:1m;  
  134.     #    ssl_session_timeout  5m;  
  135.   
  136.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  137.     #    ssl_prefer_server_ciphers  on;  
  138.   
  139.     #    location / {  
  140.     #        root   html;  
  141.     #        index  index.html index.htm;  
  142.     #    }  
  143.     #}  
  144.   
  145. }  

配置完后,执行reload命令重新加载配置文件。然后进行访问图片,http://127.0.0.1:8089/036367.png 

如果能访问,说明搭建成功。接下来还需对缓存和安全性进行研究。

===============================================================================

Linux下安装nginx,需要先安装Gcc编译器、PCRE库、zlib库、OpenSSL开发库。然后再安装nginx,

解压:tar -zxvf nginx-1.3.15.tar 

编译安装命令:

./configure

make

make install

 Linux下配置nginx图片服务器:

nginx version: nginx/0.6.35

nginx启动:/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf

nginx关闭:ps -au|grep nginx

然后kill -9 进程id  或 killall -9 nginx

nginx.conf配置文件内容如下:

Xml代码  收藏代码
  1. user  root;  
  2. worker_processes  1;  
  3. worker_rlimit_nofile 65535;  
  4.   
  5. #error_log  logs/error.log;  
  6. #error_log  logs/error.log  notice;  
  7. #error_log  logs/error.log  info;  
  8.   
  9. #pid        logs/nginx.pid;  
  10.   
  11.   
  12. events {  
  13.     worker_connections  12040;  
  14. }  
  15.   
  16.   
  17. http {  
  18.     include       mime.types;  
  19.     default_type  application/octet-stream;  
  20.     #Proxy_cache_path       /pic/image_cache levels=1:2   keys_zone=cache_one:200m inactive=1d max_size=30g ;  
  21.     #log_format  main  '$remote_addr - $remote_user [$time_local] $request '  
  22.     #                  '"$status" $body_bytes_sent "$http_referer" '  
  23.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  24.   
  25.     #access_log  logs/access.log  main;  
  26.   
  27.     sendfile        on;  
  28.     #tcp_nopush     on;  
  29.   
  30.     #keepalive_timeout  0;  
  31.     keepalive_timeout  65;  
  32.   
  33.     #gzip  on;  
  34.   
  35.     server {  
  36.         listen       80;  
  37.         server_name  localhost;  
  38.   
  39.         #charset koi8-r;  
  40.   
  41.         #access_log  logs/host.access.log  main;  
  42.   
  43.         location / {  
  44.             root   html;  
  45.             index  index.html index.htm;  
  46.             #proxy_pass  http://192.168.10.223:1234;  
  47.         }  
  48.   
  49.         #error_page  404              /404.html;  
  50.   
  51.         # redirect server error pages to the static page /50x.html  
  52.         #  
  53.         error_page   500 502 503 504  /50x.html;  
  54.         location = /50x.html {  
  55.             root   html;  
  56.         }  
  57.   
  58.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  59.         #  
  60.         #location ~ .php$ {  
  61.         #    proxy_pass   http://127.0.0.1;  
  62.         #}  
  63.   
  64.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  65.         #  
  66.         #location ~ .php$ {  
  67.         #    root           html;  
  68.         #    fastcgi_pass   127.0.0.1:9000;  
  69.         #    fastcgi_index  index.php;  
  70.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  71.         #    include        fastcgi_params;  
  72.         #}  
  73.   
  74.         # deny access to .htaccess files, if Apache's document root  
  75.         # concurs with nginx's one  
  76.         #  
  77.         #location ~ /.ht {  
  78.         #    deny  all;  
  79.         #}  
  80.     }  
  81.   
  82.   
  83.     # another virtual host using mix of IP-, name-, and port-based configuration  
  84.     #  
  85.     #server {  
  86.     #    listen       8000;  
  87.     #    listen       somename:8080;  
  88.     #    server_name  somename  alias  another.alias;  
  89.   
  90.     #    location / {  
  91.     #        root   html;  
  92.     #        index  index.html index.htm;  
  93.     #    }  
  94.     #}  
  95.   
  96.   
  97.     # HTTPS server  
  98.     #  
  99.  server {  
  100.         listen       7788;  
  101.         server_name  localhost;  
  102.        # ssl on;  
  103.        # ssl_certificate  /usr/local/nginx/conf/server.crt;  
  104.        # ssl_certificate_key  /usr/local/nginx/conf/server_nopwd.key;  
  105.   
  106.         charset utf-8;  
  107.         #charset koi8-r;  
  108.   
  109.         location ~ (.jsp)|(.do) {  
  110.             proxy_pass  http://127.0.0.1:7001;  
  111.             proxy_set_header    X-Real-IP  $remote_addr;  
  112.   
  113.             proxy_set_header    Host       $host;  
  114.             proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;  
  115.             proxy_buffer_size 4k;  
  116.             proxy_buffers 4 32k;  
  117.             proxy_busy_buffers_size 64k;  
  118.             proxy_temp_file_write_size 64k;  
  119.             proxy_max_temp_file_size 512m;  
  120.         }   
  121.       }    
  122.   
  123.  server {  
  124.          listen 8089;  
  125.          server_name localhost;  
  126.   
  127.      charset utf-8;  
  128.   
  129.          location ~  .*.(gif|jpg|jpeg|png)$ {  
  130.         #allow 127.0.0.1;  
  131.         #deny all;  
  132.           
  133.   
  134.         #expires 24h;  
  135.             root /home/weblogic/pic/;  
  136.             access_log /opt/nginx/logs/log_test.log;  
  137.             proxy_store on;  
  138.             proxy_store_access user:rw group:rw all:rw;  
  139.                
  140.             proxy_redirect          off;  
  141.   
  142.             proxy_set_header        Host $host;  
  143.             proxy_set_header        X-Real-IP $remote_addr;  
  144.             proxy_set_header        X-Forwarded-For $remote_addr;  
  145.             client_max_body_size    10m;  
  146.             client_body_buffer_size 1280k;  
  147.             proxy_connect_timeout   900;  
  148.             proxy_send_timeout      900;  
  149.             proxy_read_timeout      900;  
  150.             proxy_buffer_size       1024k;  
  151.             proxy_buffers           40 1024k;  
  152.             proxy_busy_buffers_size 1024k;  
  153.             proxy_temp_file_write_size 1024k;  
  154.         proxy_temp_path        /home/weblogic/pic/;  
  155.         #Proxy_cache_path       /pic/;  
  156.         if ( !-e $request_filename)  
  157.             {  
  158.          proxy_pass  http://127.0.0.1:8089;  
  159.             }  
  160.               
  161.     }    
  162.     location / {  
  163.             root   html;  
  164.             index  index.html index.htm;  
  165.   
  166.         }  
  167.   
  168.         #error_page  404              /404.html;  
  169.   
  170.         # redirect server error pages to the static page /50x.html  
  171.         #  
  172.         error_page   500 502 503 504  /50x.html;  
  173.         location = /50x.html {  
  174.            root   html;  
  175.         }  
  176.   
  177.    }  
  178.   
  179. }  

 

配置过程问题汇总:

1、failed  Permission denied  权限问题

修改nginx.conf文件中

user nobody

改成:user root

2、nginx中Too many open files的问题

可参考相关博文 http://www.01happy.com/nginx-too-many-open-files/ 

http://zlr.iteye.com/blog/1961257

3、nginx recv() failed (104: Connection reset by peer) while reading response header from upstream

修改nginx.conf文件中:

 if ( !-e $request_filename)

            {

proxy_pass  http://127.0.0.1:8089;

            }

原文地址:https://www.cnblogs.com/s648667069/p/6337684.html