I'm trying to make a cdn for fonts. But it's giving cross-origin error for fonts. Though I am allowing cross-origin.
Here is my nginx config file
server {
listen 80;
root /var/www/cdn.example.com/public_html;
index index.html index.htm index.php;
server_name cdn.example.com www.cdn.example.com;
location / {
add_header Access-Control-Allow-Origin *;
try_files $uri $uri/ /404.html;
}
location ~* .(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid|js|css|wml|swf|ttf|ttc|otf|eot|woff|woff2)$ {
add_header Access-Control-Allow-Origin "*";
expires max;
}
}
Fonts files are in /var/www/cdn.example.com/public_html/fonts
The issue has been solved. It just needs some time to take effect the cross-origin.
Related
I'm trying to serve WordPress from the subdirectory site.com/blog but im facing 404, tried different configurations but none of them are working getting a 404.
The WordPress files are at /home/ubuntu/wordpress
Here is my Nginx configuration, I'm serving static files for the root domain site.com and running Laravel for backend API, I think it conflicts with the FastCGI settings, not sure though.
server {
root /home/ubuntu/mysite/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name mysite.com www.mysite.com;
access_log off;
include /etc/nginx/badbots.conf;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js|json|ttf|woff)$ {
add_header Access-Control-Allow-Origin *;
expires 365d;
add_header Cache-Control "public, max-age:24h";
}
large_client_header_buffers 4 32k;
location /blog {
alias /home/ubuntu/wordpress;
index index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
}
listen 80;
}
I'm tring to access the bundles directory (which is located in : /usr/src/app/public/bundles) of my symfony project.
But, all files in public dir can't be accessed with my navigator. eg:
Request URL: http://localhost:8080/bundles/easyadmin/app.css
Request Method: GET
Status Code: 404 Not Found
The file exists...
This is my nginx config :
server {
server_name ~.*;
location / {
root /usr/src/app;
try_files $uri /index.php$is_args$args;
}
client_max_body_size 100m;
location ~ ^/index\.php(/|$) {
if ($request_method ~* "(GET|POST|PATCH|DELETE)") {
add_header "Access-Control-Allow-Origin" '*' always;
}
# Preflighted requests
if ($request_method = OPTIONS ) {
add_header "Access-Control-Allow-Origin" '*' always;
add_header "Access-Control-Allow-Methods" "GET, POST, PATCH, DELETE, OPTIONS, HEAD";
add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
return 200;
}
fastcgi_pass php:9000;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_read_timeout 600;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/src/app/public/index.php;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
I don't know what is missconfigured...
Without seeing any error log I can just guess the issue but lets check directories you are using.
First of all try to avoid root in locations.
Putting root inside of a location block will work and it’s perfectly valid. What’s wrong is when you start adding location blocks. If you add a root to every location block then a location block that isn’t matched will have no root. Therefore, it is important that a root directive occur prior to your location blocks, which can then override this directive if they need to.
The configuration should look like this:
root /usr/src/app/public/;
location / {
try_files $uri /index.php$is_args$args;
}
Your location is missing the public directory. So why do see the 404:
For this request http://localhost:8080/bundles/easyadmin/app.css with your configuration NGINX will look into /usr/src/app/ for /bundles/easyadmin/app.css. And it will not be able to find it. But /usr/src/app/public/bundles/easyadmin/app.css` will be a valid path and should result in 200 OK.
I have a small VueJS app which is located in index.html. For SEO value I've also started generating a lot of static html files in subfolders. The problem is that Nginx only routes my requests to index.html, and not to these static html files.
I'd like all requests to for instance mydomain.com/podcast/test-podcast/first-episode.html to serve /podcast/test-podcast/first-episode.html
What directive do I need to add in Nginx to add such a rule?
Here's my conf file:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /usr/share/nginx/html;
index index.html;
server_name localhost;
try_files $uri $uri/ /index.html?q=$uri&$args;
location / {
try_files $uri $uri/ #rewrites;
}
location #rewrites {
rewrite ^(.+)$ /index.html last;
}
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
# Some basic cache-control for static files to be sent to the browser
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
}
Long work but not impossible; For each page you will need to create a NGinx rules
...
location /podcast/test-podcast/first-episode.html {
root /usr/share/nginx/html/podcast/test-podcast/
index first-episode.html
}
location /blabla/seo-roxx/example.html {
root /blabla/seo-roxx/example/
index example.html
}
...
You may want to rethink you SEO strategie to avoid doing it in NGinx.
"I'm setting up an Nginx server on Hostwinds for CI project which is in a subdirectory. Where I want to make a clean URL by removing index.php. Initially, this project was running on Apache server and with the help of .htaccess file, I have made a clean URL by removing index.php. But .htaccess file not works on the Nginx server. So, tell me what codes should I use to remove index.php from URL in 'Hostwinds' Server.
In subdirectory home page of projects opens but when you click on any of its links it will redirects you to 404 page.
I have tried various solution which is available on the internet but none of them worked for me. I have used this code inside nginx.conf file.
some of them:-
1)
location /category/subcategory {
try_files $uri $uri/ /category/subcategory/index.php;
}
2)
location /subfoldername/ {
root /usr/share/nginx/www/subfoldername;
try_files $uri $uri/ /index.php?$query_string;
}
3)
location /api/ {
alias /var/www/api/;
try_files $uri $uri/ /api/index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass backend;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
4)
location /nested {
alias /var/www/nested/public;
try_files $uri $uri/ /index.php$is_args$args;
}
...http {...
server {
listen 443 ssl http2;
#listen [::]:443 ssl http2 ipv6only=off;
server_name example.com;
location / {
index index.html index.htm index.php;
try_files $uri $uri/ #backend;
}
location #backend {
include proxy_params_common;
# === MICRO CACHING ===
# Comment the following line to disable 1 second micro-caching for dynamic
HTML content
include proxy_params_dynamic;
}
# Enable browser cache for static content files (TTL is 1 hour)
location ~* \.(?:json|xml|rss|atom)$ {
include proxy_params_common;
include proxy_params_static;
expires 1h;
}
# Enable browser cache for CSS / JS (TTL is 30 days)
location ~* \.(?:css|js)$ {
include proxy_params_common;
include proxy_params_static;
expires 30d;
}
# Enable browser cache for images (TTL is 60 days)
location ~* \.(?:ico|jpg|jpeg|gif|png|webp)$ {
include proxy_params_common;
include proxy_params_static;
expires 60d;
}
# Enable browser cache for archives, documents & media files (TTL is 60 days)
location ~* \.
(?:3gp|7z|avi|bmp|bz2|csv|divx|doc|docx|eot|exe|flac|flv|gz|less|mid|midi|mka|mkv|mov|mp3|mp4|mpeg|mpg|odp|ods|odt|ogg|ogm|ogv|opus|pdf|ppt|pptx|rar|rtf|swf|tar|tbz|tgz|tiff|txz|wav|webm|wma|wmv|xls|xlsx|xz|zip)$ {
set $CACHE_BYPASS_FOR_STATIC 1;
include proxy_params_common;
include proxy_params_static;
expires 60d;
}
# Enable browser cache for fonts & fix #font-face cross-domain restriction (TTL is 60 days)
location ~* \.(eot|ttf|otf|woff|woff2|svg|svgz)$ {
include proxy_params_common;
include proxy_params_static;
expires 60d;
#add_header Access-Control-Allow-Origin *;
}
# Prevent logging of favicon and robot request errors
location = /favicon.ico {
include proxy_params_common;
include proxy_params_static;
expires 60d;
log_not_found off;
}
location = /robots.txt {
include proxy_params_common;
include proxy_params_static;
expires 1d;
log_not_found off;
}
# Deny access to files like .htaccess or .htpasswd
location ~ /\.ht {
deny all;
}
}
Initially link:- www.example.com/index.php/search-result
resulted in link:- www.example.com/search-result
I have an webapp that uses FastCGI. This app is deployed as different "sites". I have a general nginx configuration for the app, and then, I include upstrams and locations files for each site. Each site needs some .js and .css files. Right now, I have set these via the root of the general configuration file. My requirment now, is that I want to be able to use different .css and .js for different sites. So the lookup of the files, should first start at the site/location level and if not found, then search in the general root.
The general nginx conf file is:
# Change this directory depending on the server
include /etc/nginx/conf.d/app/upstreams/*.conf;
server
{
server_name 24.39.17.76;
root /var/www/app/files/;
listen 443;
ssl on;
ssl_certificate /etc/nginx/conf.d/app/ssl/new/app.pem;
ssl_certificate_key /etc/nginx/conf.d/app/ssl/new/app.com.key;
location ~* ^.+\.(css|js|jpg|jpeg|gif|png|ico|svg|woff|ttf)$ {
access_log off; # this is because otherwise, with jail2ban the user may be banned
# add_header Access-Control-Allow-Origin "*";
expires max;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
add_header Pragma public;
}
# Change this directory depending on the server
include /etc/nginx/conf.d/app/locations/*.conf;
}
Note the lines where I include all locations conf and upstreams files.
Then a particular site, has a upstream file like:
upstream siteX
{
server localhost:9021;
server localhost:9022;
server localhost:9023;
}
And a location file like this one:
location ~ ^/siteX/reps {
access_log /var/log/nginx/app/siteX_reps_access.log;
error_log /var/log/nginx/app/siteX_reps_error info;
if ($arg_service = "true") {
rewrite ^/(.*)$ /service/$1 last;
}
rewrite ^/siteX/(.*)$ /$1 break;
try_files $uri #fastcgiSiteX;
}
location #fastcgiSiteX {
include fastcgi_params;
fastcgi_param REQUEST_URI $uri?$args;
fastcgi_pass fastcgiSiteX;
fastcgi_next_upstream error invalid_header http_500;
}
The html of my webpage that includes the css and js is like this:
<script type="text/javascript" src="/JQDevelopmentLibrary/jQuery.js"></script>
<link rel="stylesheet" type="text/css" href="/TBSDevelopmentLibrary/css/bootstrap.css"/>
But I can easily add a prefix if that helps with any possible solution, like:
<script type="text/javascript" src="/siteX/webfiles/JQDevelopmentLibrary/jQuery.js"></script>
<link rel="stylesheet" type="text/css" href="/siteX/webfiles/TBSDevelopmentLibrary/css/bootstrap.css"/>
SO...the general files are under /var/www/app/files/. However, I would like that for siteX, the files are first searched in another path, say /var/www/siteX/files and only if not found, searched in the general one.
One solution I tried but didn't work is to define this try_files expression:
try_files /siteX/webfiles$uri $uri #fastcgiSiteX;
And then, I put all the folders and files in $document_root/siteX/webfiles, that is, /var/www/app/files/siteX/webfiles/. I thought this solution should work, but it didn't.
Any ideas?
Thanks in advance,
its kind of a hack but it can be done.
location ~ ^/siteX/reps {
access_log /var/log/nginx/app/siteX_reps_access.log;
error_log /var/log/nginx/app/siteX_reps_error info;
if ($arg_service = "true") {
rewrite ^/(.*)$ /service/$1 last;
}
rewrite ^/siteX/(.*)$ /$1 break;
try_files $uri #hack;
}
location #hack {
root /var/www/siteX/files;
try_files $uri $uri/ #fastcgiSiteX;
}
location #fastcgiSiteX {
include fastcgi_params;
fastcgi_param REQUEST_URI $uri?$args;
fastcgi_pass fastcgiSiteX;
fastcgi_next_upstream error invalid_header http_500;
}