![将 nginx 设置为 apache 的代理;一切都很好,但是 nginx 不提供媒体服务](https://linux22.com/image/510387/%E5%B0%86%20nginx%20%E8%AE%BE%E7%BD%AE%E4%B8%BA%20apache%20%E7%9A%84%E4%BB%A3%E7%90%86%EF%BC%9B%E4%B8%80%E5%88%87%E9%83%BD%E5%BE%88%E5%A5%BD%EF%BC%8C%E4%BD%86%E6%98%AF%20nginx%20%E4%B8%8D%E6%8F%90%E4%BE%9B%E5%AA%92%E4%BD%93%E6%9C%8D%E5%8A%A1.png)
我已经将其设置为 nginx 代理请求并将 django 请求发送到 apache 并本身提供媒体服务。
以下记录了我的设置:
Nginx 配置:/etc/nginx/nginx.conf
user www-data;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
include /etc/nginx/sites-enabled/*;
}
=====
ngnix 代理/etc/nginx/proxy.conf
============
proxy_redirect off;
proxy_set_header Host $host;
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 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
===========
Nginx 服务器文件:/etc/nginx/sites-enabled/some-name.txt
==========
server {
listen 208.109.252.110:80;
server_name netconf;
autoindex on;
access_log /home/site/server_logs/nginx_access.log;
error_log /home/site/server_logs/nginx_error.log;
location / {
proxy_pass http://127.0.0.1:80/;
include /etc/nginx/proxy.conf;
}
location /site_media/ {
root /home/site/folder/static;
}
}
==========
Nginx 很好地代理了请求并将所需的请求传递给 apache,但不提供媒体服务。在最后一个服务器文件中,位置 site_media 根本没有提供服务。:(
我觉得一切都很完美。有什么问题吗?
提前致谢。
答案1
添加如下内容:
location ~* ^.+\.(jpg|jpeg|gif|png|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|wav|bmp|rtf|js|ico|swf)$ {
root /home/site/folder/static;
expires 7d;
然后,您将从不同的地方获得带有这些扩展名的文件。您还可以为其指定不同的到期日期和压缩率等。
完整的文件看起来如下:
server {
listen 208.109.252.110:80;
server_name netconf;
autoindex on;
access_log /home/site/server_logs/nginx_access.log;
error_log /home/site/server_logs/nginx_error.log;
location / {
proxy_pass http://127.0.0.1:80/;
include /etc/nginx/proxy.conf;
}
location location ~* ^.+\.(jpg|jpeg|gif|png|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|wav|bmp|rtf|js|ico|swf)$ {
root /home/site/folder/static;
expires 7d;
}
}
您可能还想为文件添加更多选项。请查看以下链接,了解动态和静态内容的一些特殊选项以及一些总体安全问题。