When uploading static files to my server using Nginx as the web server my css, javascript, and google fonts are not working as they do when testing the site on localhost.
I'm running Nginx in a docker container using the base image.
Dockerfile
FROM nginx
COPY example.com.conf /etc/nginx/nginx.conf
COPY build /etc/nginx/html/
nginx.conf
user nginx;
events {
worker_connections 4096; ## Default: 1024
}
http {
server {
listen 80;
listen [::]:80;
server_name example.com;
include /etc/nginx/mime.types;
root /etc/nginx/html;
index index.html;
location ~ \.css {
add_header Content-Type text/css;
}
location ~ \.js {
add_header Content-Type application/x-javascript;
}
location / {
try_files $uri /index.html =404;
}
}
}
Can someone tell me whats wrong with my conf?
Also when viewed on Chrome the console logs this Resource interpreted as Stylesheet but transferred with MIME type text/plain
Some other SO post I looked at:
SO post
SO post
With the help of this SO answer and the comments I was able to get it working. If my answer doesn't help you I suggest you look at that one when running Nginx in a docker container.
For me it was moving the include /etc/nginx/mime.types; and adding sendfile on;
outside my server block and in the http block
My example.com.conf now looks like this:
user nginx;
events {
worker_connections 4096; ## Default: 1024
}
http {
include /etc/nginx/mime.types;
sendfile on;
server {
listen 80;
listen [::]:80;
server_name example.com;
root /etc/nginx/html;
index index.html;
location ~ \.css {
add_header Content-Type text/css;
}
location ~ \.js {
add_header Content-Type application/x-javascript;
}
location / {
try_files $uri /index.html =404;
}
}
}
Related
I followed some answers here did the changes to the location block to serve the static assets. Seems no luck.
/etc/nginx/sites-enabled/default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
proxy_pass http://127.0.0.1:3000;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location ~* /(assets|fonts|swfs|images)/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
}
Can someone please help me how to fix this issue?
If your assets' folders are inside /var/www/html, for example /var/www/html/images/test.jpg, you want to use this syntax:
location ^~ /(assets|fonts|swfs|images)/ {
access_log /var/log/nginx/test.com.access_proxy.log;
error_log /var/log/nginx/test.com.error_proxy.log;
root /var/www/html/;
internal;
}
I'm trying to serve my rails application using Puma and Nginx. When ever I got to the page it renders the default nginx page. I've tried with two different configurations. This first fails with "upstream" directive is not allowed here. The second warns Starting nginx: nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
(1) overwrite nginx.conf
nginx.conf
upstream puma {
server unix:///home/deploy/apps/exelon-api/shared/tmp/sockets/rails-api-puma.sock;
}
server {
listen 80 default_server deferred;
# server_name example.com;
root /home/deploy/apps/rails-api/current/public;
access_log /home/deploy/apps/rails-api/current/log/nginx.access.log;
error_log /home/deploy/apps/rails-api/current/log/nginx.error.log info;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #puma;
location #puma {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://puma;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 10M;
keepalive_timeout 10;
}
(2) using sites enabled
nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_requests 100;
keepalive_timeout 65;
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_proxied any;
gzip_vary off;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/rss+xml application/atom+xml text/javascript application/javascript application/json text/mathml;
gzip_min_length 1000;
gzip_disable "MSIE [1-6]\.";
variables_hash_max_size 1024;
variables_hash_bucket_size 64;
server_names_hash_bucket_size 64;
types_hash_max_size 2048;
types_hash_bucket_size 64;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
sites-enable/rails-api_production
upstream puma_rails-api_production {
server unix:/home/deploy/apps/rails-api/shared/tmp/sockets/rails-api-puma.sock fail_timeout=0;
}
server {
listen 80;
client_max_body_size 4G;
keepalive_timeout 10;
error_page 500 502 504 /500.html;
error_page 503 #503;
server_name localhost rails-api.local;
root /home/deploy/apps/rails-api/current/public;
try_files $uri/index.html $uri #puma_rails-api_production;
location #puma_rails-api_production {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://puma_rails-api_production;
# limit_req zone=one;
access_log /home/deploy/apps/rails-api/shared/log/nginx.access.log;
error_log /home/deploy/apps/rails-api/shared/log/nginx.error.log;
}
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location #503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
location ~ \.(php|html)$ {
return 405;
}
}
Second error practically says that you have two server sections defined with listen 80 (which practically means 0.0.0.0:80 where 0.0.0.0 is equal to "any address") and localhost in server_name.
So, action you have to take depends on what you want to achieve:
Have some other app as default
If you want to have your app to be accessible alongside some other apps you have (or want to have in future) on server by some hostname, let's say example.com, you have to simply remove localhost from server_name in the entry you added.
In this case, you will have to access your app by one of the names you have specified for it in server_name ( example.com), neither localhost nor server IP.
Note that if you still have to make sure domain you've specified resolves to the server address.
If it is on your own local machine and you want to be able to access your app for development purposes (testing how it works with nginx, for example) you can
add row 127.0.0.1 name.here (for example, 127.0.0.1 example.com).
If it is on some server you own and you want others to be able to access app by the same domain, you have to buy/register it (if it has not been registered by someone else yet, it happens) and create the A DNS record pointing to your app server IP.
Make your app default
If you want this app to be default on the server, you can:
Find other server sections defined with listen 80 default; (so, if they don't have default or listen any other port like 8080 or 443 you can safely leave them) and remove the default from there - there can be only one default server section per port and IP combination.
Change listen 80; in your server section to listen 80 default;. This will tell nginx that you want this server section to handle all requests not catched by other section.
This will allow you to access app by IP and point any domain to it without specifying it in server_name section. You still need to register or buy domain to be able to point it to app.
I've deployed rails app with Capistrano to VPS, and when I try to access it with "APP_NAME.com", I see standard Nginx's "It works!" page.
I've tried to remove index.html file from /var/www folder, now I see folders in it: apps, log and tmp.
In nginx.conf I have:
user nginx web;
pid /var/run/nginx.pid;
error_log /var/www/log/nginx.error.log;
events {
worker_connections 1024;
accept_mutex off;
use epoll;
}
http {
include mime.types;
types_hash_max_size 2048;
server_names_hash_bucket_size 64;
default_type application/octet-stream;
access_log /var/www/log/nginx.access.log combined;
sendfile on;
tcp_nopush on; # off may be better for *some* Comet/long-poll stuff
tcp_nodelay off; # on may be better for some Comet/long-poll stuff
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 0;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
gzip_proxied expired no-cache no-store private auth;
gzip_comp_level 9;
gzip_types text/plain text/xml text/css
text/comma-separated-values
text/javascript application/x-javascript
application/atom+xml;
upstream app_server {
server unix:/var/www/apps/APP_NAME/socket/.unicorn.sock fail_timeout=0;
}
server {
pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
add_header "" "";
}
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }
location /ngx_pagespeed_statistics {
allow 127.0.0.1; allow 5.228.169.73; deny all;
}
location /ngx_pagespeed_global_statistics {
allow 127.0.0.1; allow 5.228.169.73; deny all;
}
pagespeed MessageBufferSize 100000;
location /ngx_pagespeed_message {
allow 127.0.0.1; allow 5.228.169.73; deny all;
}
location /pagespeed_console {
allow 127.0.0.1; allow 5.228.169.73; deny all;
}
charset utf-8;
listen 80 default deferred; # for Linux
client_max_body_size 4G;
server_name _;
keepalive_timeout 5;
root /var/www/apps/APP_NAME/current/public;
try_files $uri/index.html $uri.html $uri #app;
location ~ ^/(assets)/ {
root /var/www/apps/APP_NAME/current/public;
expires max;
add_header Cache-Control public;
}
location #app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /var/www/apps/APP_NAME/current/public;
}
}
}
Am I missing something in nginx.conf or in other files?
Also, when I stop nginx server, it doesn't make sense, I see the same page.
I'm not good in deploying apps to server, it's my first time without Heroku, so I don't know, what exactly you need to know, to help in my problem. So, if you need any additional info, ask, I'll add it to question.
Thanks!
So, I've found the solution from this answer. The problem was with Apache web server, which was running in background and prevent Nginx to run on 80 port.
I've stopped Apache server and restart Nginx, and everything now works ok:
sudo apachectl stop
sudo restart nginx
Your must use passenger and set in nginx config something like this:
http {
passenger_root /home/deployer/.rvm/gems/ruby-1.9.3-p327/gems/passenger-3.0.18;
passenger_ruby /home/deployer/.rvm/wrappers/ruby-1.9.3-p327/ruby;
...
or use HTTP server for Rack, as example Unicorn, and set in nginx config:
upstream app_server {
server unix:/path/to/.unicorn.sock fail_timeout=0;
...
server {
proxy_pass http://app_server;
...
I'm hosting an API for a mobile app developed in Rails 4 on DigitalOcean using the One-Click Ubuntu 12.10 Installation droplet. Therefore nginx and Unicorn are preconfigured and running. When I try to open a file from Rails public folder on my web browser, it works.
But when I try to use any of the other paths defined in the routes.rb file I don't get any answer. My requests are loading until they time out. This changes when I type rails s -e production on the server – then everything works as expected. Therefore it must be a problem with nginx or something. What am I missing?
Here's my nginx sites-enabled/default file content (I don't know if that's the thing to look for, but maybe):
server {
listen 80;
root /home/rails/public;
server_name _;
index index.htm index.html;
location / {
try_files $uri/index.html $uri.html $uri #app;
}
#location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|mid$
location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rt$
try_files $uri #app;
}
location #app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
UPDATE: Here is my nginx.conf file in case this helps:
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
events { worker_connections 1024; }
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
gzip_types text/plain text/xml text/css text/comma-separated-values;
upstream app_server { server 127.0.0.1:8080 fail_timeout=0; }
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
UPDATE 2: And here my unicorn.conf file:
listen "127.0.0.1:8080"
worker_processes 2
user "rails"
working_directory "/home/rails"
pid "/home/unicorn/pids/unicorn.pid"
stderr_path "/home/unicorn/log/unicorn.log"
stdout_path "/home/unicorn/log/unicorn.log"
I had the same issue and running bundle install on the root of my app (on digitalocean droplet) resolved my issue. I think I have to build that into my capistrano script
I'm running nginx in a Virtual Machine using NAT and I'm having redirection issues when I access it from the host machine.
Works as expected
http://localhost:8080/test/index.htm: works.
http://localhost:8080/test/: works.
Doesn't work as expected
http://localhost:8080/test: redirects to http://localhost/test/ . This is not what I want. (notice it strips the port number)
What I've tried
Based on what I've googled, I tried server_name_in_redirect off; and rewrite ^([^.]*[^/])$ $1/ permanent;, both with no success.
My default.conf:
server {
listen 80;
server_name localhost;
# server_name_in_redirect off;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
location ~ \.php$ {
# rewrite ^([^.]*[^/])$ $1/ permanent;
root /usr/share/nginx/html;
try_files $uri =404;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
I posted a possible solution to this problem on serverfault; reproduced here for convenience:
If I understand the question correctly, you want to automatically serve, without using a 301 redirect, http://example.com/foo/index.html when the request is for http://example.com/foo with no trailing slash?
Basic solution that works for me
If so I've found this try_files configuration to work:
try_files $uri $uri/index.html $uri/ =404;
The first $uri matches the uri exactly
The second $uri/index.html matches a directory containing the index.html where the last element of the path matches the directory
name, with no trailing slash
The third $uri/ matches the directory
The fourth =404 returns the 404 error page if none of the preceding patterns match.
Taken from Serverfault answer
My updated version
If you add in the server block:
index index.html index.htm;
And modify try_files to look like this:
try_files $uri $uri/ =404;
It should work too.
A somewhat simpler solution, that worked for me, is to disable absolute redirects with absolute_redirect off; as in the following example:
server {
listen 80;
server_name localhost;
absolute_redirect off;
location /foo/ {
proxy_pass http://bar/;
}
If I run curl on on http://localhost:8080/foo, I can see that the Location header in the redirect HTTP response is given as /foo/ and not http://localhost/foo/.
$ curl -I http://localhost:8080/foo
HTTP/1.1 301 Moved Permanently
Server: nginx/1.13.8
Date: Tue, 03 Apr 2018 20:13:28 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: /foo/
From that, I assume any web-browser would do the right thing with the relative location. Tested on Chrome and it works fine.
try :
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
if (-d $request_filename) {
rewrite [^/]$ $scheme://$http_host$uri/ permanent;
}
}
}
Try changing
server_name localhost;
# server_name_in_redirect off;
to
server_name localhost:8080;
server_name_in_redirect on;