I am new to nginx server. I tried to set a new url "/images/" for serving images. I edited the bi.site file in site-enabled folder.
server {
listen *:80;
access_log /var/log/myproject/access_log;
location / {
proxy_pass http://127.0.0.1:5000/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /images/ {
root /www/myproject/files_storage;
}
}
And in /www/myproject/files_storage location i put a temp.txt file.
When i put http://www.main_url/images/temp.txt it shows 404 not found. What am i doing wrong ? Did i miss something important ?
this:
location /images/ {
root /www/myproject/files_storage;
}
results in /www/myproject/files_storage/images path, it would be obvious if you setup error_log. So use "alias" directive instead of "root"
http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
Related
I'm trying to redirect a location to another location, but for some reason it doesn't work. My nginx service is running under a Docker container on 8080:80 port.
I have /portal that shows my web application correctly and I would like nginx redirects from / to /portal. I've tried some approaches using location / or location = /, but with no success.
The /portal can be accessed normally by
http://localhost:8080/portal
but for some reason when I access
http://localhost:8080/
the browser redirects to
https://localhost/portal
instead of redirecting to:
http://localhost:8080/portal
Locations:
upstream webportal {
server portal_container:8080;
}
location / {
return 301 /portal;
}
location /portal {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://webportal;
}
I was able to fix the problem by adding absolute_redirect off; to my location:
location / {
absolute_redirect off;
return 301 /portal;
}
Today I used two servers for nginx, the content of nginx.conf as follows:
#192.168.2.98
server {
listen 8091;
location ^~ /ttank {
alias /develop/servers-running/front/vue-public/dist;
index index.html;
try_files $uri $uri/ /ttank/index.html;
}
}
#192.168.2.97
location /ttank {
proxy_pass http://192.168.2.98:8091;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
}
I can access the 192.168.2.98:8091/ttank by enter address: http://192.168.2.98:8091/ttank in chrome, I also can access 192.168.2.98's ttank by entering the address http://192.168.2.97/ttank/, but when I change the addres http://192.168.2.97/ttank/ into http://192.168.2.97/ttank, my chrome entered into waiting status forever, the only difference between two addresses is the last "/", I don't know how to modify the config file for removing the last "/" when accessing ttank by 192.168.2.97?
Try usinge a rewrite rule to get rid of the ending slashes
location /ttank {
rewrite ^/(.*)/$ /$1 break;
...;
...;
proxy_pass ...;
}
it should do it
I have two sites in sites-enabled for nginx:
1) project - this is essentially the top level domain - mysite.com
server {
listen 80;
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static {
alias /home/www/flask-deploy/project/static/;
}
}
2) blog - this is for a blog, that is accessible via: mysite.com:8080
server {
listen 8080;
location blog/ {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static {
alias /home/www/flask-deploy/blog/static/;
}
}
Nginx has accepted both configurations, but when I visit 1 i get all of the text, but no css, or images.
If I visit 2 i get everything working fine.
What am I doing wrong?
Thank you
For the alias directive to work correctly, the URI in the location and the URI in the alias directive should both end in / or neither end in /. The algorithm seems to substitute one string for the other.
So you should probably write:
location /static {
alias /home/www/flask-deploy/project/static;
}
However, when the last element(s) of the alias match the location, the root directive is preferred (see this):
location /static {
root /home/www/flask-deploy/project;
}
Your location blog/ looks incorrect, prefix locations should always begin with a /.
Given an Nginx configuration roughly like this:
upstream A {
server aa:8080;
}
upstream B {
server bb:8080;
}
server {
listen 80;
location #backendA {
proxy_pass http://A/;
}
location #backendB {
proxy_pass http://B/;
}
location / {
# This doesn't work. :)
try_files #backendA #backendB =404;
}
}
Basically, I would like Nginx to try upstream A, and if A returns a 404, then try upstream B instead, and failing that, return a 404 to the client. try_files does this for filesystem locations, then can fall back to a named location, but it doesn't work for multiple named locations. Is there something that will work?
Background: I have a Django web application (upstream A) and an Apache/Wordpress instance (upstream B) that I would like to coexist in the same URL namespace for simpler Wordpress URLs: mysite.com/hello-world/ instead of mysite.com/blog/hello-world/.
I could duplicate my Django URLs in the Nginx locations and use wordpress as a catch-all:
location /something-django-handles/ {
proxy_pass http://A/;
}
location /something-else-django-handles/ {
proxy_pass http://A/;
}
location / {
proxy_pass http://B/;
}
But this violates the DRY principle, so I'd like to avoid it if possible. :) Is there a solution?
After further googling, I came upon this solution:
location / {
# Send 404s to B
error_page 404 = #backendB;
proxy_intercept_errors on;
log_not_found off;
# Try the proxy like normal
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://A;
}
location #backendB {
# If A didn't work, let's try B.
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://B;
# Any 404s here are handled normally.
}
I need to rewrite any root subdomain requests and append locale params if they aren't already there. e.g. -> de.example.com needs to be rewritten as -> de.example.com/?locale=de. then I proxy it off to the app.
2 questions:
1) is this the correct approach? or should I be using regex instead here? (new to this
so if other problems, please lmk)
2) is there a way to log things inside the location block? Having trouble getting same config working on another server, logging would help. (e.g logging what args is if it isn't matching, or if it matches on another location block).
It only needs to happen on the root page so this is my current config
#existing default (nonsubdomain block)
server {
server_name _;
root /var/www/web_app;
try_files $uri/index.html $uri.html $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;
}
}
#just added for subdomain
server {
server_name de.example.com;
root /var/www/web_app;
location / {
try_files $uri/index.html $uri.html $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;
}
location = / {
if ($args != locale=de ){
rewrite ^ $scheme://de.example.com/?locale=de permanent;
}
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;
}
}
1) is this the correct approach? or should I be using regex instead here? (new to this so if other problems, please lmk)
You should use $arg_locale != de instead of $args != locale=de. Look at the docs: http://nginx.org/en/docs/http/ngx_http_core_module.html#var_arg_
2) is there a way to log things inside the location block? Having trouble getting same config working on another server, logging would help. (e.g logging what args is if it isn't matching, or if it matches on another location block). It only needs to happen on the root page so this is my current config
Debug log: http://nginx.org/en/docs/debugging_log.html