【问题标题】:Not working Bootstrap site on nginx + php-fpm在 nginx + php-fpm 上无法使用 Bootstrap 站点
【发布时间】:2017-08-01 19:49:44
【问题描述】:

我的站点目录中有下一个文件夹和文件:

 |-css/
 |-js/
 |-fonts/
 |-index.php

我看到我的 index.php 没有样式

nginx 配置文件:

server { 
    listen  80;
    root /usr/share/nginx/html;
    index index.php index.html index.htm;
    server_name xx.xx.xxx.xxx;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass phpfpm:9000; 
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

我需要如何在 nginx 中编写我的静态位置?

【问题讨论】:

  • 你将不得不更好地澄清你的问题。

标签: php nginx


【解决方案1】:
server {
   listen 80;

   root /var/www/phpMyAdmin;
   index index.php index.html index.htm;

   server_name phpmyadmin.dev;

   location / {
      try_files $uri $uri/ /index.php?$query_string;
   }

   location ~ \.php$ {
      try_files $uri /index.php =404;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
   }

}

【讨论】:

  • 我在 docker-compose 中使用 nginx,所以我们有不同的根路径
  • 更改根路径没问题,我认为需要添加“fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;”这个
【解决方案2】:

如果您的静态目录与 index.php 位于同一目录中,您可以像这样添加它们

<link rel="shortcut icon" href="/css/style.css">

<link rel="shortcut icon" href="your_domain/css/style.css">

如果您想要这些文件的特殊目录,您可以在您的 nginx 配置中添加一个位置。您可以为它们添加自定义缓存策略。

server { 
    listen  80;
    root /usr/share/nginx/html;
    index index.php index.html index.htm;
    server_name xx.xx.xxx.xxx;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    // You can add a custom location for a directory
    location /css {
         root different_root_for_css;
    }

    // You can add a custom location for file types
    location ~* \.(js|jsx|jpg|jpeg|png|gif|ico|css)$ {
        // root path for these files
        root different_root_for_these_files;
        //Custom cache policy
        expires 365d;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass phpfpm:9000; 
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

您可以查看nginx resources了解更多信息。

另一种方法;一些网站将子域用于静态文件。这样,如果您打算使用 cdn 提供商,您可以更改您的子域 dns,它也可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 2016-05-18
    相关资源
    最近更新 更多