I have a webapp running in kubernetes. I want to serve static files, css in my case, from nginx pod. From the application I define css file location like this:
<link rel="stylesheet" href="assets/css/stylesheet.css" type="text/css">
When building docker image I copy over css file to www/media/ and in nginx config I point to that:
FROM nginx
RUN rm /etc/nginx/conf.d/default.conf
COPY config/default.conf /etc/nginx/conf.d/default.template
COPY assets/ /www/media
EXPOSE 80
Here's nginx config:
server {
listen 0.0.0.0:80;
server_name localhost;
location / {
proxy_pass http://${FLASK_APP}:8080/;
}
location ~ /assets {
root /www/media;
}
}
I have confirmed that the file can be found on nginx pod under /www/media/css/stylesheet.css, however I cannot reach it neither from the browser nor the application itself.
The error I get is this:
GET http://192.168.99.106:30604/assets/css/stylesheet.css net::ERR_ABORTED 404 (Not Found)
/assets should point to www/media where the directory with stylesheet are kept, correct?
What am I misunderstanding?
Not sure if this is the solution, but hopefully some things to try.
Change your docker file to
COPY assets /www/media
In your comments you've said that you can see the files in /www/media. But you're trying to access them in /assets. Have you configured this in nginx correctly? Perhaps try this
location /assets/ {
alias /www/media/;
}
Final thing I would mention is permissions. What are the permissions of the files in the container? ls -la will tell you this. They should be 755 I believe for Nignx.
Hope this helps you.
Ok, I figured it out.
Here's my Nginx config to serve the files:
server {
listen 0.0.0.0:80;
server_name localhost;
location / {
proxy_pass http://${FLASK_APP}:8080/;
}
location ~ \.css {
root /www/media;
}
}
I also changed my docker a bit:
FROM nginx
RUN rm /etc/nginx/conf.d/default.conf
COPY config/default.conf /etc/nginx/conf.d/default.template
COPY assets /www/media/assets
RUN chown -R nginx:nginx /www/
EXPOSE 80
With the above config in place css is being served without any issues.
Related
I have some slate docs as website and would like to serve them on the internal server, through a subdomain as follows: internal-docs.mysite.com. For the record, accessing mysite.com shows the "nginx is running propertly" page.
I've created a config file with following path and name: /etc/nginx/sites-available/internal-docs.mysite.com:
server {
listen 80;
server_name internal-docs.mysite.com;
root /var/www/docs-internal;
index index.html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
}
And of course, I've put the files in /var/www/docs-internal. And then I made a symlink to the uppershown config file in the /etc/nginx/sites-enabled dir:
internal-docs.mysite.com -> ../sites-available/internal-docs.mysite.com
Then I reload nginx -s reload but "this site can't be reached" error is what I get when accessing the URL.
The setup and configuration look correct to me (according to the guidelines I've followed), so that's why I'm in a dead end, sort of...
It seems you forgot the Listen directive. Try the following:
server {
listen 80;
server_name internal-docs.mysite.com;
root /var/www/docs-internal;
index index.html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
}
If that does not work, check:
That Nginx user has read permission to the site content. For example if your Nginx user is www and you have root access, do the following:
# su www
$ cat /var/www/docs-internal/index.html
If that fails, ensure the location has correct ownership and permissions. Note that for a user to be able to browser a directory, that directory must have the execute bit for that user or user group.
That Nginx user has read permission on file ../sites-available/internal-docs.mysite.com. For example if your Nginx user is www and you have root access, do the following:
# su www
$ cat /etc/nginx/sites-available/internal-docs.mysite.com
If that fails, ensure that the config files have correct ownership. Note: normally Nginx master process is run by root, and that process spawns sub-processes run as Nginx user, so permissions on config files are unlikely to be the problem.
That maybe your config file name should end with ".conf" (on my server I have the following line: include conf.d/*.conf; so it will NOT load any conf file ending with ".com".
That Nginx tries to load files in ../sites-available/ in its main config file. Maybe it does not and looks instead in the conf.d directory (the default).
That you can do a ping and nslookup on the subdomain. If you cannot, then you have to fix that first (DNS, firewall...).
For the sake of others - the configuration I wrote was correct, and my problem was in 2 things:
I had to remove the listen 80 directive, since there is another configuration file already, that specifies that nginx should listen on port 80. One should not tell nginx twice to listen on the same port, even if it's in two separate configuration files
Permissions on the /var/www/docs-internal folder. Opening a folder requires x (execute) permissions, while opening a file requires r (read) perm. I had to provide the according permissions to all the folders in this hierarchy, so that the content could be open globally (from everyone), which is basically accessing it from the browser.
Sorry for noob question, I suck at Ubuntu.
I have just installed nginx in a Ubuntu server with:
sudo apt-get update
sudo apt-get -y install nginx
It successfully built. I'm trying to change the index page, so I have modified my /usr/share/nginx/html/index.html, and then tried all of these:
sudo service nginx stop
sudo service nginx start
sudo service nginx restart
But when I refresh the root page on my browser it still shows the old page.
This is what the index.html looks like:
I have checked my /etc/nginx/nginx.conf, but don't find anything particular there.
What could I be missing?
If you had checked vhost, you knowned, root directory is /var/www/html...
vhost is in /etc/nginx/sites-available and /etc/nginx/sites-enabled (sites-enabled is symlink).
The correct configuration file for NGINX on Debian is :
/var/www/html/index.nginx-debian.html
If you update this file, the changes will be reflected immediately, without a start/stop or restart.
sudo service nginx stop
sudo service nginx start
sudo service nginx restart
I have same problem before, then after updating nginx conf by moving 'root' from 'server/location' to 'server', it works well. Nginx config file :
server {
listen 443 ssl;
server_name localhost;
root /usr/share/nginx/html/rdist;
location /user/ {
proxy_pass http://localhost:9191;
}
location /api/ {
proxy_pass http://localhost:9191;
}
location /auth/ {
proxy_pass http://localhost:9191;
}
location / {
index index.html index.htm;
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
}
I'm using Docker to serve my simple WordPress website. A nginx container and a wordpress container. Simple setup:
upstream wordpress_english {
server wordpress_en:80;
}
server {
listen 80;
server_name my_domain.com www.my_domain.com;
location / {
proxy_pass http://wordpress_english;
}
}
Problem: Static files (css, js and images) are not loaded.
The output from the browser console shows a 404:
http://wordpress_english/wp-content/themes/twentyfifteen/genericons/genericons.css?ver=3.2
Its easy to spot the problem: The browser looks for the static files at wordpress_english (the nginx upstream name), instead of my_domain.com
How can I fix this problem?
This is not a nginx problem, but a WordPress problem.
Solution:
In wp-config.php, add the following two lines:
define('WP_HOME','http://my_domain.com');
define('WP_SITEURL','http://my_domain.com');
During the WordPress installation, WordPress automatically sets WP_HOME to nginx upstream name. The above solution overwrites the default setting.
Seems to be an issue in your nginx config file.
When declaring your server my_domain you provide location / with proxy_pass wordpress_english. I don't know a lot on nginx, but I don't see any declaration of path in your server my_domain and is root is linked to wordpress_english. Seems normal that he is looking for files in wordpress_english and not in you server. (In fact, I guess he is looking in your server but your server tells to look in wordpress).
Not sure about it cause I don't know well nginx and proxy_pass functions.
In my /etc/nginx/nginx.conf file I have a server block like this:
server {
location /testme/ {
root /home/username/sites;
index index.html index.htm;
}
}
When I visit http:localhost/testme/ in my browser, shouldn't files be served from /home/username/sites/testme/ directory, where I have my simple index.html? Instead I get 404 Not Found Error. What I am doing wrong here.
NOTE: I did sudo nginx -s reload after changing configuration.
Question: How do I serve content from a proxy and static content from well just have nginx serve it up.
Background: Hello I'm setting up a really simple nginx application and for the most part it works but I'd like to better understand nginx what's going on. I've followed the documentation but in the real world I run into some weeds.
here's the config:
server {
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8080;
}
}
The above code works the thing is I have some static content that I'd just like nginx to serve w/o bothering to send that back to the proxy.
Directory structure:
/var/www/env/application/app/ <--this is the proxied location
/var/www/env/application/app/static <-- i'd like to just have nginx serve these files w/o the proxy.
When I edit my server settings and try to add:
location /static {
root /var/www/env/application/app/static/;
}
everything breaks
What am I doing wrong here?
You don't understand how root works. Look at the documentation again and then try to understand why it should be:
location /static {
root /var/www/env/application/app;
}