我的site-directory中有下一个文件夹和文件:
|-css/
|-js/
|-fonts/
|-index.php
我看到我的index.php没有样式
nginx conf文件:
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中编写静态位置?
答案 0 :(得分:0)
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;
}
}
答案 1 :(得分:0)
如果静态目录与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,它也可以正常工作。