簡體   English   中英

nginx vhost,問題服務 .css 作為靜態文件或動態文件

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

我在滿足我的要求時遇到了問題:我想要兩件事;

  1. https://www.test-boutique.vm/store.css被路由到 PHP 應用程序,因為文件內容是由應用程序流式傳輸的;
  2. https://www.test-boutique.vm/static/css/basic.css由文件系統提供,因為它存在於文件系統上;

我的虛擬主機是:

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;
}

有了這個版本:

  1. /store.css文件運行良好(由 PHP 應用程序生成)
  2. /static/css/basic.css試圖由 PHP 應用程序提供服務,而不是直接從文件系統提供文件(該文件肯定存在於此路徑下)

從 vhost 中刪除css部分時 ( location ~ \\.(php|htm|css)$ { TO NEW location ~ \\.(php|htm)$ {

  1. /store.css文件試圖作為靜態資產並以 404 結束(請求未傳遞給應用程序)
  2. /static/css/basic.css正確提供

請問我錯過了什么?

不是像您在此處匹配所有 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