When a user visits website.com I want the contents of the /home/user/project/frontend to be served (a single page app)
When a URL comes in that matches /assets, it should look into the /home/user/project/backend/staticfiles folder, which contains collected static files from django.
To the above effect, below is my Nginx conf
upstream backend {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name website.com www.website.com;
root /home/user/project/frontend;
index index.html index.htm;
location /assets {
root /home/user/project/backend/staticfiles;
}
location / {
try_files $uri $uri/ /index.html;
}
location ~^/(admin|api|auth|swagger|webhook) {
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://backend;
}
}
Intuitively, the above should work, right? However, when a request like website.com/assets/admin/css/base.css comes in, according to the logs, Nginx goes looking into the /home/user/project/frontend folder, which is NOT what the configuration is saying it should do.
2022/08/24 22:42:29 [error] 18946#18946: *1 open() "/home/user/project/frontend/assets/admin/css/base.css" failed (2: No such file or directory), client: xx.xx.xx.xx, server: xx.xx.xx.xx, request: "GET /assets/admin/css/base.css HTTP/1.1", host: "website.com"
On a different server, I have the EXACT same configuration, EXACT same django-react setup, EXACT nginx version, EXACT everything, yet, on this fresh installation, with the SAME configuration, Nginx is absolutely ignoring the location /assets { } block like it doesn't even exist.
Is there a typo or something somewhere? Because nginx -t also indicates the file is fine.
When I do website.com/assets/admin/css/base.css/ (note trailing forward slash), Nginx attempts THEN to look into the right folder, although it's off too.
2022/08/24 23:01:39 [error] 18946#18946: *4 "/home/user/project/backend/staticfiles/assets/admin/css/base.css/index.html" is not found (2: No such file or directory), client: xx.xx.xx.xx, server: xx.xx.xx.xx, request: "GET /assets/admin/css/base.css/ HTTP/1.1", host: "website.com"
Maybe the whole approach I'm using is buggy, unreliable and or inconsistent. Maybe there's a better approach (although as I type this, I have the EXACT same conf working as expected on another server).
Please enlighten me.
Here's an updated conf file, that I can confirm to be working on my new setup.
From this article:
The modifier ^~ in the following location block results in a case sensitive regular expression match. Therefore the URI /images or /images/logo.png will be matched but stops searching as soon as a match is found.
upstream backend {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name xx.xx.xx.xx;
root /home/user/project/frontend;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
location ^~ /assets/ {
root /home/user/project/backend;
}
location ~^/(admin|api|auth|swagger|webhook) {
...
}
}
And so with the above, when Nginx gets a request like
example.com/assets/admin/base.css, it points to the folder
/home/user/project/backend/assets/admin/base.css
Basically, it appends the location block URL to the root path.
In my django settings.py, I have these
STATIC_URL = "/assets/"
STATIC_ROOT = os.path.join(BASE_DIR, "assets")
Related
I have configured three locations for a nginx reverse proxy:
location / {
root /var/www/html;
index index.html;
}
location /login {
proxy_pass http://127.0.0.1:9080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /app {
rewrite ^/app/(.*)$ /$1 last;
proxy_pass https://10.11.12.13/1020/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
The server listening on port 9080 redirects to the route /app/{generated subpath}. The server on IP 10.11.12.13 processes the request on {generated subpath}. Therefore I remove the prefix path /app with a corresponding rewrite rule, then proxying the request to that server's /1020 endpoint.
For some reason, the nginx reverse proxy doesn't take the 10.11.12.13 upstream server but tries to find the path locally:
8888#8888: *470 open() "/var/www/html/html/createCustomer" failed (2: No such file or directory), client: x.x.x.x, server: 10.10.10.10, request: "GET /app/html/createCustomer?tokenId=0xC00FF3 HTTP/1.1", host: "10.10.10.10"
Instead of last I believe you are looking for break. From the rewrite documentation
last
stops processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI;
The starts a search for a new location matching the changed URi is what is happening as you are removing the /app/ part which then matches the / location.
break
stops processing the current set
I am very new with nginx and bitbucket server setup. I have an nginx server setup in front of bitbucket instance (running on port 7990). Below is my configuration of /etc/nginx/sites-available/default (not /etc/nginx/nginx.conf)
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 / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location /bitbucket/ {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:7990/;
sendfile off;
expires 0;
add_header Cache-Control private;
add_header Cache-Control no-store;
add_header Cache-Control no-cache;
}
}
Now if I hit
http://my_ip/bitbucket
it forwards to
http://my_ip/bitbucket/setup
page, but fails to load css and js files.
When I see the html source of setup page, it includes some js/css files' absolute paths while importing (e.g. link type="text/css" rel="stylesheet" href="/s/277823dc49fd32854c324f87e345f7f6-CDN/-196139424/aef68c7/1/1b615eee93091f36273c8450f7d72556/_/download/contextbatch/css/_super/batch.css"). This seems like nginx is looking for the css files in "s" folder under its own directory.
I also checked that
http://my_ip/bitbucket/s/277823dc49fd32854c324f87e345f7f6-CDN/-196139424/aef68c7/1/1b615eee93091f36273c8450f7d72556/_/download/contextbatch/css/_super/batch.css
Url returns the page in browser.
Please let me know how to load css/js files. I also want to know why the css url is so ugly(it seems that it includes some random numbers).
I would like to redirect specifc subdomains of my domain to my backend as
prefixes of the URL that is passed to the backend. This is because I have a single server and I do not want to have to handle the multiple domains in the backend due to increased complexity.
Hence if I have:
sub1.domain.com => domain.com/sub1/
sub1.domain.com/pathname => domain.com/sub1/pathname
sub1.domain.com/pathname?searchquery => domain.com/pathname?searchquery
and so forth.
So far what I have come up with is the following:
server {
charset utf8;
listen 80;
server_name
domain.com,
sub1.domain.com,
sub2.domain.com,
sub3.domain.com,
sub4.domain.com,
sub5.domain.com;
# Default
if ($host ~ ^domain\.com) {
set $proxy_uri $request_uri;
}
# Rewrites
if ($host ~ (.*)\.domain\.com) {
set $proxy_uri $1$request_uri;
}
location / {
expires 1s;
proxy_pass http://node:8080$proxy_uri; #node is an internally listed host (docker container)
proxy_set_header Host domain.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_valid 200 1s;
}
}
But unfortunately all I get is a 502: Bad Gateway with the following log, 2017/06/11 12:49:18 [error] 6#6: *2 no resolver defined to resolve node, client: 136.0.0.110, server: domain.com:8888,, request: "GET /favicon.ico HTTP/1.1", host: "sub1.domain.com:8888", referrer: "http://sub1.domain.com:8888/" Any idea how I can achieve my goal? Any help would be greatly appreciated :)
Cheers!
It seems I wasn't so far from the answer - adding an upstream block before the server block was sufficient to finalize the config to the desired effect.
upstream backend {
server node:8080;
keepalive 8;
}
I also had to slightly modify the proxy pass line to the following:
proxy_pass http://backend$proxy_uri;
The problem must likely have been one related to how NGINX parses the proxy pass urls - if anyone reading this can provide an insight into the reason, please edit this answer!
I'm getting below error while trying to access static files files, though I'm trying to accessa static/js/main.js file, nginx is looking for static/js/main.js/index.html.. below is the error for the same.
2016/01/20 03:31:01 [error] 2410#0: *1
"/home/ubuntu/vnv/near/near/static/js/main.js/index.html" is not
found (20: Not a directory), client: 162.111.50.122, server: my.domain.com, request: "GET /static/js/main.js/ HTTP/1.1", host: "my.domain.com"
Below is my server block..
server {
listen 80;
server_name my.domain.com;
# location ~ ^/(static/.*) {
location ~ /static {
root /home/ubuntu/vnv/near/near; # near consist of static directory
}
location ~ / {
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_pass http://near_server;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Also it is appending / at the end of the URI, what could be the issue here?
Note: I've gone through most of the answers related to this, but I coudn't find proper solution for this situation.
If the request URI ends in a /, nginx will treat it as a directory and apply the indexing rules. Unless overridden, nginx looks for an index.html file.
From the error message we see that the GET request was for /static/js/main.js/. So the question is who appended the / to the request URI?
If you now look into the access log, you will see a line containing GET /static/js/main.js/ HTTP/1.1 404 which lists the HTTP response code.
If the line above contains GET /static/js/main.js HTTP/1.1 3xx then nginx added the extra /. If there is no such line, then the client asked for the wrong URL and you need to be looking for the solution to the problem elsewhere.
Unrelated to your question: Why are you using regular expression location blocks when prefix location blocks seem more appropriate?
You should probably be using:
location /static { ... }
location / { ... }
Your existing configuration is inefficient and matches URIs with /static embedded anywhere within the string, such as: /something/static.php.
See this document for location syntax and the processing rules.
I have (1) domain and two paths configured:
mysite.com
blog.mysite.com
I'm trying to configure a wordpress blog on my existing mysite.com nginx server,
mysite.com has a sites-enabled config of:
server {
listen 80;
server_name *.mysite.com;
root /home/ubuntu/virtualenv/mysite/mysite/myapp/;
access_log /home/ubuntu/virtualenv/mysite/error/access.log;
error_log /home/ubuntu/virtualenv/mysite/error/error.log warn;
connection_pool_size 2048;
fastcgi_buffer_size 4K;
fastcgi_buffers 64 4k;
location /static {
alias /home/ubuntu/virtualenv/mysite/mysite/myapp/static/;
}
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 10;
proxy_connect_timeout 10;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
blog.mysite.com has a sites-enabled config of:
server {
listen 80;
server_name blog.mysite.com;
root /home/ubuntu/virtualenv/blog;
index index.php;
#### errors
access_log /home/ubuntu/virtualenv/blog/error/access.log;
error_log /home/ubuntu/virtualenv/blog/error/error.log warn;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
fastcgi_buffer_size 4K;
fastcgi_buffers 64 4k;
#### locations
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
the error.log file in blog.mysite.com is showing an error of:
2014/01/02 02:50:05 [crit] 29036#0: *1 connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: xx.xxx.xx.xxx, server: blog.mysite.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "blog.mysite.com"
Anyone know why this isn't working? I tried some research, but can't seem to find the solution.
Thank you!
If anyone run's into this error when trying to add a subdomain or domain while configuring nginx with phpfm - follow the steps in this post: https://askubuntu.com/questions/114076/ubuntu-php5-fpm-unix-socket
The gist:
sudo vim /etc/php5/fpm/pool.d/www.conf.
Look for the line listen = 127.0.0.1:9000 and change it to something like listen = /var/run/php5-fpm.sock. After doing so, restart PHP FPM:
sudo /etc/init.d/php5-fpm restart
Hope this helps someone :)
Actually it's not really related to subdomains, you just didn't use the right configuration, your fpm was listening to a port instead of a sock file, it wouldn't have worked even without the subdomain,
your main site was working because it was passing to port 8001
proxy_pass http://127.0.0.1:8001;
Your subdomain could have worked if you passed to port 9000
proxy_pass http://127.0.0.1:9000;
But the way you fixed it is better, using sock files is better than port.
Just wanted to explain that it wasn't because of the subdomain.