There are next folders and files in my site-directory:
|-css/
|-js/
|-fonts/
|-index.php
I see my index.php without styles
nginx conf file:
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;
}
}
How I need to write my static locations in nginx?
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;
}
}
If your static directories is in the same directory as index.php you can add them like
<link rel="shortcut icon" href="/css/style.css">
or
<link rel="shortcut icon" href="your_domain/css/style.css">
If you want a special directory for these files you can add a location to your nginx configuration. And you can add a custom cache policy for them.
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;
}
}
You can check nginx resources for more information.
Another method; some websites use subdomains for static files. This way if you plan to use a cdn provider, you can change your subdomain dns and it will work same.