搭建Nginx图片服务器
2023-09-11 14:20:17 时间
搭建Nginx图片服务器
Part-I 安装Nginx
- 安装PCRE
- 下载 ngx_cache_purge 并解压,用来清除缓存
- 下载Nginx并解压
- cd nginx-1.7.7
- 编译,--prefix使用默认值,则nginx安装在/usr/local/nginx
./configure --user=www --group=www --add-module=../ngx_cache_purge-1.0 --with-http_stub_status_module --with-http_ssl_module make && make install
Part-II 配置
vim /usr/local/nginx/conf/nginx.conf,并编辑如下:
user www www;
worker_processes 8;
error_log /data3/nginx/error.log crit;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /usr/local/nginx/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
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;
# proxy_temp_path 和 proxy_cache_path 必须在同一分区
proxy_temp_path /data0/proxy_temp_dir;
# 设置web缓存区名称为cahche_one,内存缓存空间大小为200M,1天没有被访问的内容自动清除硬盘缓存空间大小为300G
proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
# upstream backend_server{
# server 192.168.1.121:80 weight=1 max_fail=2 fail_timeout=30s;
# server 192.168.1.122:80 weight=1 max_fail=2 fail_timeout=30s;
# server 192.168.1.123:80 weight=1 max_fail=2 fail_timeout=30s;
# }
#以下为缓存服务器
log_format cache '***$time_local \n'
' $upstream_cache_status \n'
' $remote_addr, $http_x_forwarded_for \n'
' Cache-Control: $upstream_http_cache_control \n'
' Expires: $upstream_http_expires \n'
' "$request"($status) \n'
' "$http_user_agent" \n';
server {
listen 80;
server_name 192.168.1.120;
location / {
proxy_cache cache_one;
# 对不同的HTTP状态码设置不同的缓存时间
proxy_cache_valid 200 304 12h;
#以域名、URI、参数组合成web服务器的key值,Ngnix根据key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
#此处跳转到真实图片服务器
proxy_pass http://192.168.1.120:8080;
access_log /data3/nginx/cache.log cache;
expires 1d;
}
location ~ /purge(/.*){
#设置只允许指定的ip或ip段才可以清除url缓存
#allow 127.0.0.1;
#allow 192.168.0.0/16;
#deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
# #扩展名为.php、.jsp、.cig结尾的动态应用程序不缓存
# location ~.*\.(php|jsp|cgi)?$
# {
# proxy_set_header Host $host;
# proxy_set_header X-Forwarded-For $remote_addr;
# proxy_pass http://backend_server;
# }
access_log off;
#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;
#}
}
#真实的图片服务器
server{
listen 8080;
server_name localhost;
location /{
root /data0/images/;
}
#访问日志,一般都off掉
access_log /data3/nginx/access.log combined;
}
# 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图片服务区搭建完毕。 在/data0/images/下放入一张图片 200.jpg测试之 访问 http://192.168.1.120/200.jpg, cache_log记录如下:
***12/Nov/2014:16:15:26 +0800
MISS
192.168.1.19, -
Cache-Control: -
Expires: -
"GET /200.jpg HTTP/1.1"(200)
"Mozilla/5.0 (X11; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0"
***12/Nov/2014:16:15:38 +0800
HIT
192.168.1.19, -
Cache-Control: -
Expires: -
"GET /200.jpg HTTP/1.1"(200)
"Mozilla/5.0 (X11; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0"
相关文章
- Linux中Nginx服务器的部署和配置
- 干货 | Docker 还可以搭建Web服务器nginx ?这么宝藏的吗?
- linux下安装EJBCA 搭建私有CA服务器
- 网络安全-域服务器(二)
- 在 Ubuntu 16.04 为 Nginx 服务器安装 LEMP 环境(MariaDB,PHP 7 并支持 HTTP 2.0)
- Nginx漏洞修复:SSL/TLS 服务器瞬时 Diffie-Hellman 公共密钥过弱
- windows下在计算机上远程登陆云服务器操作
- Nginx 报错: nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory) 的解决方法
- nginx flv点播服务器搭建
- nginx服务器安装及配置文件详解
- 获取用户访问服务器的真实ip的nginx设置
- Nginx与服务器集群在它的配置文件中的配置
- NTP服务器配置
- Centos虚拟机SVN的安装和使用http方式访问svn服务器
- Nginx学习---企业级nginx环境搭建
- 利用Nginx搭建http和rtmp协议的流媒体服务器[转]
- nginx+nginx-rtmp-module+ffmpeg搭建流媒体服务器
- BI:HTML集成IBM Cognos Report,并使用Nginx作为静态资源服务器
- NVIDIA与服务器制造商合作推进AI云计算
- nginx搭建文件服务器
- 服务器集群自动化部署工具,自动生成 Redis+Tomcat+Nginx相关需要文件,自动配置好依赖jar包和配置文件,简单快速搭建集群环境
- 使用net模块创建tcp服务器
- HappyAA服务器部署笔记1(nginx+tomcat的安装与配置)
- Linux 之 LNMP服务器搭建-Nginx