nginx 搭建图片资源服务器小编这里涉及2个版本:windows下 和 linux下
首先去nginx官方网站下载nginx安装包
http://nginx.org/en/download.html
可看到当前nginx最新版本,小编采用的是nginx-1.9.10
windows版本:
通过nginx官网下载nginx的windows版本安装包后 直接解压到指定目录可看到
每个包文件所代表的意义在前面章节已经有所介绍这里就不做过多描述了
打开conf文件夹下nginx.conf文件
修改用户使用权限,开放user 如果不开放会报错404,追加最大连接数
修改server块
修改监听端口号:8088;
指定图片存放路径,可使用系统默认,也可以指定
指定代理临时路径,图片标红处
代理访问地址
以上都可以通过配置域名进行直接绑定访问
完整配置文件
user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; worker_rlimit_nofile 65535; #pid logs/nginx.pid; events { worker_connections 65535; } 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; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plainapplication/x-javascript text/css application/xml; gzip_vary on; server { listen 8088; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location ~ .*\.(gif|jpg|jpeg|png)$ { expires 24h; root D:/imgs/;#指定图片存放路径 access_log D:/BasicEnvironment/nginx-1.9.10/logs/images.log;#图片 日志路径 proxy_store on; proxy_store_access user:rw group:rw all:rw; proxy_temp_path D:/imgs/;#代理临时路径 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:8088;#代理访问地址 } } 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; # } #} }
修改完成后启动nginx.exe
访问http://127.0.0.1:8088 查看nginx是否监听并成功启动
访问:http://127.0.0.1:8088/tempupload/temp20170907/0a9862d7043445d68c5cb270d47db166.png
OK 以上 windows下搭建nginx 图片服务器就搞定啦
linux下:
linux下其实和windows下没有太大的差异,只需要主要用户权限是否开放就好,下面我介绍一种我们在做反向代理及负载均衡情况下如何搭建,使nginx做代理转发废话不多说 直接上图,通过upstream做负载,通过ip_hash轮训算法去判断转发到哪个服务上,这里小编只采用一台虚拟机进行配置,实际上不管是一台还是多台都是适用的,因为转发的时候 实际上是通过upstream的名称进行转发
user root; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; worker_rlimit_nofile 655350; #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; upstream blog { server 192.168.153.20 ; ip_hash; } server { listen 80; server_name picture.sht18.com; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream blog { server 192.168.153.20 ; ip_hash; } server { listen 80; server_name picture.sht18.com; location ~ .*\.(gif|jpg|jpeg|png)$ { expires 24h; root /root/imgs/;#指定图片存放路径 access_log /root/nginx/logs/images.log;#图片 日志路径 proxy_store on; proxy_store_access user:rw group:rw all:rw; proxy_temp_path /root/imgs/;#代理临时路径 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://blog;#代理访问地址 } } } }
剩下的 就和windows下是一样的