配置nginx能够获取图片(实战操作)
1. yum 方式安装nginx
2.我的个人配置文件test.conf 放至目录:/etc/nginx/conf.d/
3.个人配置文件内容:
server {
listen 8080;
client_max_body_size 10240m; # 录像及文件上传大小限制
keepalive_timeout 1000;
client_header_timeout 1000; #等待客户端的头部超时时间为60秒
client_body_timeout 1000; #等待客户端的主体超时时间为60秒
server_name test.qinxiansheng.cn 127.0.0.1 10.2.166.3;
access_log /var/log/nginx/test.log;
location /cheng {
#支持跨域访问
add_header Access-Control-Allow-Origin * always;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
#add_header Access-Control-Allow-Headers 'Origin, X-Requested-With, Content-Type, Accept, Platform, Token';
add_header Access-Control-Allow-Headers '*';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
alias /usr/share/nginx/html;
index reset.html;
}
}
4. 我的图片放至目录: /usr/share/nginx/html/
比如放至了图片:1.jpg
5. 给图片增加读写权限:chmod 666 1.jpg
root:/usr/share/nginx/html# ll
total 368
-rw-rw-rw- 1 qin qin 141852 Jun 24 09:37 1.jpg
6. 在test.conf中增加如下内容:
location ~ .*\.(gif|jpg|jpeg|png)$ {
expires 24h;
#root /usr/share/nginx/html/;#指定图片存放路径
alias /usr/share/nginx/html/;#指定图片存放路径
access_log /tmp/images.log;#日志存放路径
proxy_store on;
proxy_store_access user:rw group:rw all:rw;
proxy_temp_path /usr/share/nginx/html/;#图片访问路径
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;
#if ( !-e $request_filename)
#{
# proxy_pass http://127.0.0.1;#默认80端口
#}
}
7. 执行语法和配置检测: nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
打印上述内容则表示本次修改没有问题:
8. 执行nginx重载配置的命令:
nginx -s reload
9.在浏览器中访问:nginx所在机器10.2.166.3
http://10.2.166.3/1.jpg