简体   繁体   English

nginx vhost,问题服务 .css 作为静态文件或动态文件

[英]nginx vhost, problem service .css as static file or dynamic file

I have problems matching my requirements: I want 2 things;我在满足我的要求时遇到了问题:我想要两件事;

  1. https://www.test-boutique.vm/store.css to be routed to the PHP application because the file content is streamed by the app; https://www.test-boutique.vm/store.css被路由到 PHP 应用程序,因为文件内容是由应用程序流式传输的;
  2. https://www.test-boutique.vm/static/css/basic.css to be served from the filesystem because it exists on it; https://www.test-boutique.vm/static/css/basic.css由文件系统提供,因为它存在于文件系统上;

My vhost is :我的虚拟主机是:

server {
    listen 443;

    server_name www.test-boutique.vm;

    root /srv/app/public/front;
    index index.php;


    location / {
        # try to serve file directly, fallback to index.php
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # css are for the files generated by the application (store.css)
    location ~ \.(php|htm|css)$ {
        try_files $uri $uri/ /index.php$is_args$args;
        fastcgi_pass unix:/var/run/php-fpm.app.sock;
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_param HTTPS on;
        fastcgi_param APP_ENV dev;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~* \.(js|css|bmp|png|jpg|jpeg|gif|swf|ico)$ {
        try_files $uri =404;
        log_not_found off;
        access_log off;
        expires 7d;
        add_header Cache-Control public;
        add_header Cache-Control must-revalidate;
    }

   rewrite ^/media/(.*)$ https://test.cloud/$http_host/media/$1 permanent;
   rewrite ^/img/(.*)$ https://test.cloud/$http_host/img/$1 permanent;

  access_log /var/log/nginx/fov4_access_log;
  error_log /var/log/nginx/fov4_error_log;
}

With this version:有了这个版本:

  1. /store.css file works well (generated by the PHP application) /store.css文件运行良好(由 PHP 应用程序生成)
  2. /static/css/basic.css is trying to be served by the PHP application instead of serving the file directly from the filesystem (the file exists for sure under this path) /static/css/basic.css试图由 PHP 应用程序提供服务,而不是直接从文件系统提供文件(该文件肯定存在于此路径下)

When removing the css part from the vhost ( location ~ \\.(php|htm|css)$ { TO NEW location ~ \\.(php|htm)$ {从 vhost 中删除css部分时 ( location ~ \\.(php|htm|css)$ { TO NEW location ~ \\.(php|htm)$ {

  1. /store.css file is trying to be served as a static asset and ends up 404 (the request is not passed to the application) /store.css文件试图作为静态资产并以 404 结束(请求未传递给应用程序)
  2. /static/css/basic.css is served correctly /static/css/basic.css正确提供

What am I missing please ?请问我错过了什么?

Instead of matching all css files like you do here: location ~ \\.(php|htm|css)$ { , try matching that one css file that you need to have generated by PHP:不是像您在此处匹配所有 css 文件: location ~ \\.(php|htm|css)$ { ,而是尝试匹配您需要由 PHP 生成的那个 css 文件:

location ~ \.(php|htm)$ {
  # you php-fpm config here
}

location ~* \.(js|css|bmp|png|jpg|jpeg|gif|swf|ico)$ {
   # your static files config here
}

location = /store.css {
   # you php-fpm config here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM