how can i achive this functionality
```server {
listen 8080;
server_name web-main;
location / {
if ( IP=$(hostname -i) === '172.17.0.1' ) {
proxy_pass http://172.17.0.1:1234;
}else{
proxy_pass http://host.docker.internal:1234;
}
}
}
Related
I have setup a docker environment for testing nginx reverse proxy.
Instead of setting up a subdomain for every backend, i would like to have 1 subdomain with a location pointing to the individual backend:
while this works as expected:
server {
listen 80;
server_name dashboard.test.lan;
location / {
proxy_pass http://172.17.0.1:82;
}
}
server {
listen 80;
server_name gitlab.test.lan;
location / {
proxy_pass http://172.17.0.1:83;
}
}
server {
listen 80;
server_name portainer.test.lan;
location / {
proxy_pass https://172.17.0.1:9443;
}
}
...this does not work:
server {
listen 80;
server_name proxy.test.lan;
location /dashboard {
proxy_pass http://172.17.0.1:82;
}
location /gitlab {
proxy_pass http://172.17.0.1:83;
}
location /portainer {
proxy_pass https://172.17.0.1:9443;
}
}
Maybe you guys can give me a hint or explanation why this does not work.
I have the following nginx configuration:
upstream front {
server localhost:4000;
}
upstream back {
server localhost:8000;
}
server {
server_name my_domain.com;
listen 80;
location ~ /api($|/.*) {
rewrite ^/api($|/.*) /VirtualHostBase/http/my_domain.com:80/Intk/VirtualHostRoot/_vh_api$1 break;
proxy_pass http://back;
}
location ~ / {
add_header Cache-Control "public";
expires +1m;
proxy_pass http://front;
}
location ~ /orgs/.+$ {
return 301 http://my_domain.com/orgs;
}
location ~* manage_ {
deny all;
}
location ~ \.php$ {
return 410;
access_log off;
}
location ~ ^/(wp-admin|wp-content) {
return 410;
access_log off;
}
location = /nginx_stub_status {
stub_status on;
allow 127.0.0.1;
deny all;
}
}
server {
server_name www.my_domain.com;
listen 80;
access_log off;
return 301 http://my_domain.com$request_uri;
}
I cannot understand what is wrong with it, specifically with the 3rd location block.
If I write my_domain.com/orgs/some-org, shouldn't it redirect to my_domain.org/orgs?
It's not doing that, it acts as if that location block wasn't there.
From my understanding, nginx should fulfill the request with the most specific match possible, and it should be the one in the 3rd location block.
http://example.com/orgs will be matched by location ~ / {} instead of location ~ /orgs/.+$ {}. Consider placing rewrite outside of location block, and use location / {} (non-regex without ~) as your fallback.
server {
rewrite ~ ^/orgs/.+ /orgs permanent;
# fallback
location / {
}
}
i run a streaming service via Nginx server ,
i want to run the stream ( format m3u8) via only 1 domain
server {
listen 80;
server_name www.mydomain.com;
if ($host != "www.mydomain.com") {
return 403;
}
}
here when i test the link for example : http://ip_server_stream/live/1.playlist.m3u8 on browser it give Forbideen 403 error on all domains
if i put only
server {
listen 80;
server_name www.mydomain.com;
}
it works on all domains and VLC
i want only the link http://ip_server_stream/live/1.playlist.m3u8 works on www.mydomain.com not other domains or vlc
my nginx version is 1.7.5
this is my nginx confige file
this is my config file , it give error 403 on all domains even if i have an IF condition
worker_processes 8;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1991;
allow play all;
application live {
allow play all;
live on;
hls on;
hls_nested on;
hls_path /HLS/live;
}
}
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
if ($http_host != "www.mydomain.com") {
return 403;
}
location /live {
index index.html index.htm;
types {
application/vnd.apple.mpegurl m3u8;
}
alias /HLS/live;
add_header Cache-Control no-cache;
}
location / {
root html;
index index.html index.htm;
}
}
}
Let's make two servers. One of them is dummy just return 403 response.
http {
# dummy server.
server {
listen 80 default_server;
location / {
return 403;
}
}
# main server.
server {
listen 80;
server_name www.mydomain.com;
# PLEASE WRITE YOUR CONFIGURATION.
}
}
Try this.
if ($http_host != "www.mydomain.com") {
return 403;
}
This is my nginx conf file.
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:9000;
}
location /some/directory {
proxy_pass http://localhost:8998;
}
}
server {
listen 80;
server_name example2.com;
location / {
proxy_pass http://localhost:8999;
}
}
For some reason example.com and example2.com are working, but example.com/some/directory is not.
localhost:9000 & localhost:8999: are harp.js sites, they have they own routing, and work properly, both locally and on the proxy-ed domains (example.com & example2.com).
localhost:8998: is a golang api, it works locally and also if I access example.com:8998 or example2.com:8998.
Is there something wrong with the conf?
EDIT: added more info to the question.
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:9000/;
}
location /some/directory {
proxy_pass http://localhost:8998/;
}
}
server {
listen 80;
server_name example2.com;
location / {
proxy_pass http://localhost:8999/;
}
}
Try this, adding the last / in the proxy_pass's should work.
I have two different server_name in nginx.conf file:
First one as:
server_name ~^(?<subdomain>.+)\.nithinveer\.com$;
location /
{
proxy_pass http://192.168.6.190/Profiles/$subdomain/default.aspx$request_uri/;
access_log /var/log/nginx/true.log;
}
Another one as
server_name ~^(?<subdomain>.+)\.nithinveer\.com\.(?<extension>)$;
location /extension
{
proxy_pass http://192.168.6.190;
access_log /var/log/nginx/false.log;
}
Now the thing is I want to use both the server_name based on the in the server_name. If there is no extension with the server_name it should go to first location. If there is an extension, it should go to second location.
But while running the nginx, it is not moving into the second server_name
Can anyone please find some solution for this...?
I thought a solution as(may be wrong).
server_name ~^(?<subdomain>.+)\.nithinveer\.com\.(?<extension>.+)$;
if($<extension> == NULL)
{
location /
{
proxy_pass http://192.168.6.190/Profiles/$subdomain/default.aspx$request_uri/;
access_log /var/log/nginx/true.log;
}
}
else
{ location /
{
proxy_pass http://192.168.6.190;
access_log /var/log/nginx/false.log;
}
But the syntax with the if statement throws an error.
There's no else directive in nginx.
Also, you don't need to duplicate the location either.
try this:
server {
server_name ~^(?<subdomain>.+).nithinveer\.com(?<extension>\..+)$;
location / {
This will match hosts terminating with ".com"
if ($extension = "")
{
proxy_pass http://192.168.6.190/Profiles/$subdomain/default.aspx$request_uri/;
access_log /var/log/nginx/true.log;
}
This will match hosts with something after ".com", e.g: "foo.nithinveer.com.me", the $extension will be ".me"
if ($extension != "")
{
proxy_pass http://192.168.6.190;
access_log /var/log/nginx/false.log;
}
}
}
Also, consider that if is evil.
This is the version with a safe usage of it:
server_name ~^(?<subdomain>.+).nithinveer\.com(?<extension>\..+)$;
location / {
error_page 418 = #with_extension;
#This will match hosts with something after ".com",
# e.g: "foo.nithinveer.com.me", the $extension will be ".me"
if ($extension != "")
{
return 418;
}
# This will match hosts terminating with ".com"
proxy_pass http://192.168.6.190/Profiles/$subdomain/default.aspx$request_uri/;
access_log /var/log/nginx/true.log;
}
location #with_extension {
proxy_pass http://192.168.6.190;
access_log /var/log/nginx/false.log;
}