openresty 中mime.types 文件缺失问题,无法展示图片

       看技术群有人问这个:“图片不展示了,直接下载了,怎么设置nginx“,之前刚开始学习nginx时遇到过,然后 使用 openresty+lua在做网关时遇到过,这里还是记录下吧。

        原因解释:“想要 nginx自动处理静态资源,然后图片是自动下载了,而不是展示在网页中”,这是因为 nginx的配置不对,少了MIME配置文件,导致 服务器不知该如何设置HTTP Response的Content-Type,从而浏览器就一波下载了。

  搞清楚原因了,那么就设置 mime.types 呗,

然而,会提示这个,

nginx: [emerg] open() "/data/server/conf/mime.types" failed (2: No such file or directory) in /data/server/conf/api_nginx.conf

这个说 没找到这个配置文件,mime.types 文件并不神奇,其实就是一个对应文件,创建mime.types,写入如下:

参考这个:https://github.com/h5bp/server-configs-nginx/blob/master/mime.types

types {

# Audio
audio/midi mid midi kar;
audio/mp4 aac f4a f4b m4a;
audio/mpeg mp3;
audio/ogg oga ogg opus;
audio/x-realaudio ra;
audio/x-wav wav;

# Images
image/bmp bmp;
image/gif gif;
image/jpeg jpeg jpg;
image/png png;
image/svg+xml svg svgz;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/webp webp;
image/x-icon ico cur;
image/x-jng jng;

# JavaScript
application/javascript js;
application/json json;

# Manifest files
application/x-web-app-manifest+json webapp;
text/cache-manifest manifest appcache;

# Microsoft Office
application/msword doc;
application/vnd.ms-excel xls;
application/vnd.ms-powerpoint ppt;
application/vnd.openxmlformats-officedocument.wordprocessingml.document docx;
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx;
application/vnd.openxmlformats-officedocument.presentationml.presentation pptx;

# Video
video/3gpp 3gpp 3gp;
video/mp4 mp4 m4v f4v f4p;
video/mpeg mpeg mpg;
video/ogg ogv;
video/quicktime mov;
video/webm webm;
video/x-flv flv;
video/x-mng mng;
video/x-ms-asf asx asf;
video/x-ms-wmv wmv;
video/x-msvideo avi;

# Web feeds
application/xml atom rdf rss xml;

# Web fonts
application/font-woff woff;
application/font-woff2 woff2;
application/vnd.ms-fontobject eot;
application/x-font-ttf ttc ttf;
font/opentype otf;

# Other
application/java-archive jar war ear;
application/mac-binhex40 hqx;
application/pdf pdf;
application/postscript ps eps ai;
application/rtf rtf;
application/vnd.wap.wmlc wmlc;
application/xhtml+xml xhtml;
application/vnd.google-earth.kml+xml kml;
application/vnd.google-earth.kmz kmz;
application/x-7z-compressed 7z;
application/x-chrome-extension crx;
application/x-opera-extension oex;
application/x-xpinstall xpi;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-bittorrent torrent;
application/zip zip;

application/octet-stream bin exe dll;
application/octet-stream deb;
application/octet-stream dmg;
application/octet-stream iso img;
application/octet-stream msi msp msm;
application/octet-stream safariextz;

text/css css;
text/html html htm shtml;
text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/vtt vtt;
text/x-component htc;
text/x-vcard vcf;

}

然后引入就ok。
以下展示图片的设置
location ~ .*.(gif|jpg|jpeg|png)$ {
expires 24h;
root /data/www/image;#指定图片存放路径

proxy_store on;
proxy_store_access user:rw group:rw all:rw;
proxy_temp_path /data/www/image;#图片访问路径
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:8080;#默认80端口
}
}
原文地址:https://www.cnblogs.com/guixiaoming/p/8358408.html