Nginx搭建图片服务器
Nginx下载地址:http://nginx.org/en/download.html
本例下载的是window版本nginx-1.6.1
以下是我本机操作说明:
下载完后,解压,并把它放到D:\tools\nginx-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:\tools\nginx-1.6.1>nginx -c D:\tools\nginx-1.6.1\conf\nginx.conf
接下来我们配置图片服务器:
1、在本地建了一个D:\resourcesfile\images文件夹,里面放了一张png测试图片。
2、配置nginx.conf文件,配置文件内容如下:
Xml代码
- #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 8089;#端口号
- server_name localhost;#本机
- charset utf-8;
- #access_log logs/host.access.log main;
- location ~ .*\.(gif|jpg|jpeg|png)$ {
- expires 24h;
- root D:/resourcesfile/images/;#指定图片存放路径
- access_log D:/tools/nginx-1.6.1/logs/log_test.log;#图片路径
- proxy_store on;
- proxy_store_access user:rw group:rw all:rw;
- proxy_temp_path D:/resourcesfile/images/;#图片路径
- proxy_redirect off;
- proxy_set_header Host 127.0.0.1;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- 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;
- if ( !-e $request_filename)
- {
- proxy_pass http://127.0.0.1:8089;#代理访问地址
- }
- }
- location / {
- root html;
- index index.html index.htm;
- }
- 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 localhost;
- # ssl_certificate cert.pem;
- # ssl_certificate_key cert.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;
- # }
- #}
- }
配置完后,执行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代码
- user root;
- worker_processes 1;
- worker_rlimit_nofile 65535;
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- #pid logs/nginx.pid;
- events {
- worker_connections 12040;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- #Proxy_cache_path /pic/image_cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g ;
- #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;
- #proxy_pass http://192.168.10.223:1234;
- }
- #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 7788;
- server_name localhost;
- # ssl on;
- # ssl_certificate /usr/local/nginx/conf/server.crt;
- # ssl_certificate_key /usr/local/nginx/conf/server_nopwd.key;
- charset utf-8;
- #charset koi8-r;
- location ~ (\.jsp)|(\.do) {
- proxy_pass http://127.0.0.1:7001;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header Host $host;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_buffer_size 4k;
- proxy_buffers 4 32k;
- proxy_busy_buffers_size 64k;
- proxy_temp_file_write_size 64k;
- proxy_max_temp_file_size 512m;
- }
- }
- server {
- listen 8089;
- server_name localhost;
- charset utf-8;
- location ~ .*\.(gif|jpg|jpeg|png)$ {
- #allow 127.0.0.1;
- #deny all;
- #expires 24h;
- root /home/weblogic/pic/;
- access_log /opt/nginx/logs/log_test.log;
- proxy_store on;
- proxy_store_access user:rw group:rw all:rw;
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $remote_addr;
- 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 1024k;
- proxy_buffers 40 1024k;
- proxy_busy_buffers_size 1024k;
- proxy_temp_file_write_size 1024k;
- proxy_temp_path /home/weblogic/pic/;
- #Proxy_cache_path /pic/;
- if ( !-e $request_filename)
- {
- proxy_pass http://127.0.0.1:8089;
- }
- }
- location / {
- root html;
- index index.html index.htm;
- }
- #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;
- }
- }
- }
配置过程问题汇总:
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;
}
来源:oschina
链接:https://my.oschina.net/u/1054538/blog/685954