martedì 9 ottobre 2018

Nginx, Angular 6 e FontAwesome 4.7 - Problemi Url

I Modulo npm dei FontAwesome 4.7.0 in Angular aggiunge alla path chiamante del file css l'estensione con la versione:

es: http://xxx.xxx.xx/static/fontawesome-webfont.af7ae505a9eed503f8b8.woff2?v=4.7.0

Nginx non accetta di default la tipologia di file, quindi è necessario modificare il file mime.types, aggiungendo le seguenti tipologie:

application/x-font-ttf ttc ttf;
application/x-font-otf otf;
application/font-woff  woff;
application/font-woff2 woff2;
application/vnd.ms-fontobject eot;

Questa cosa causa nella risposta standard del browser un errore OTP, a questo punto è necessario inserire un regola di rewrite su Nginx per eliminare la parte di estensione non prevista ?v=4.7.0

    location /static/ {
         alias  /opt/static/html/;
         index index.html;

        if ($request_uri ~* ^.+\.(woff2|otf|woff|tff)\?v=(.*)$){
             rewrite ^(.*)$ $uri? permanent;
         }
         try_files $uri$args $uri$args/ $uri/ /index.html =404;
         error_log /var/log/nginx/static_error.log;
         access_log /var/log/nginx/static_access.log;
     }

Esempio completo:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name jhis-int.aslto5.local;
root /opt/html/;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
location /other/ {
alias /opt/other/;
}
location /static/ {
alias /opt/static/html/;
index index.html;
# add_header X-debug-message $request_uri always;
if ($request_uri ~* ^.+\.(woff2|otf|woff|tff)\?v=(.*)$){
rewrite ^(.*)$ $uri? permanent;
}
try_files $uri$args $uri$args/ $uri/ /index.html =404;
error_log /var/log/nginx/static_error.log;
access_log /var/log/nginx/static_access.log;
}
location /api/ {
proxy_pass http://127.0.0.1:8090/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
error_log /var/log/nginx/api_error.log;
access_log /var/log/nginx/api_access.log;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
view raw nginx.conf hosted with ❤ by GitHub

Nessun commento: