I'm having troubles setting up a reverse proxy for a sub-subdomain using nginx.
I'm trying to create a configuration which allows me to resolve:
a.b.example.com ==> a.local.host.lan
Currently i am able to resolve it as follows:
a.b.example.com ==> a.b.local.host.lan
with following configuration:
server {
listen 80;
server_name ~^(.*)\.b.example\.com$;
set $servername $1;
rewrite ^(.*)$ https://$servername.local.host.lan;
}
any idea how to tokenize the $servername variable so i can split a and b?
Related
I have the following locations in my nginx config:
server {
listen 80;
server_name localhost;
location ~ ^/(?!api)(.*)/api {
alias /var/www/api/$1;
}
location /api {
alias /var/www/api/latest;
}
I am trying to match /api as the latest version, and /<version>/api as anything else. The non regex location is working fine, but I am getting a 403 on the other location.
I don't think it is anything to do with the file and permissions that are being served, as I get a 403 if I try to access
/latest/api
even though these are the same files that are served by
/api
Does anyone have an ideas about why I am getting a 403?
The nginx error is:
directory index of "/var/www/api/latest" is forbidden, client:
172.17.0.1, server: localhost, request: "HEAD /latest/api/ HTTP/1.1",
host: "localhost"
The problem is not with the regular expression, but with the use of the alias directive within a regular expression location. See this document for more.
On a related note, rather than using a negative lookahead assertion, you should use the ^~ modifier on the prefix location. See this document for more.
For example:
location ~ ^(/[^/]+)/api(.*)$ {
alias /var/www/api$1$2;
}
location ^~ /api {
alias /var/www/api/latest;
}
I want the following.
http://some.site/person1/some/path should access /home/person1/some/path (and http://some.site/person1 accesses /home/person1/index.html) and http://some.site/person2/some/path should access /home/person2/some/path (and http://some.site/person2 accesses /home/person2/index.html). There will be many personXes. It's important to use a regular expression to tell nginx where to find everything.
I tried coming up with a set of location, root and rewrite directives that would work. The closest I came was this for my sites-available/website.
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /some/default/root;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri.html $uri $uri/ =404;
}
location /person1 {
root /home/person1;
rewrite ^/person1(.*)$ $1 break;
}
}
This does what I want with all paths except for ones of the form http://some.site/person1. In this case, nginx doesn't access /home/person1/index.html like I want. Instead, the regex returns an empty string which nginx doesn't like (I see complaints in the nginx/error.log).
when you have common start root dir in /home, you can try with:
location ~* /person\d+ {
root /home;
}
How do I deny illegal host headers besides all subdomains (wildcard solution) and the main domain with nginx? When using this code below all of the subdomains stop working.
if ($host !~* ^(domain.com|*.domain.com)$ ) {
return 444;
}
My server name is:
server_name domain.com *.domain.com;
How can this be accomplished?
See if this works for you:
if ($host !~* ^(.+\.)?domain\.com$ ) {
return 444;
}
You need to escape the periods, otherwise they will be interpreted as "any character". You can also simplify the regex a bit by matching an optional subdomain prefix before "domain.com".
A cleaner solution would be to create a separate (default) server block for "other" server names:
server {
server_name .domain.com; # shorter form for "domain.com *.domain.com"
... # your main config here
}
server {
listen *:80 default_server;
server_name "";
return 444;
}
I'm trying to covert 5G Blacklist to from Apache(.htaccess) to Nginx(.conf). There is a line in .htaccess that is causing problem:
<IfModule mod_alias.c>
RedirectMatch 403 (\,|\)\+|/\,/|\{0\}|\(/\(|\.\.\.|\+\+\+|\||\\\"\\\")
</IfModule>
I have converted it to .conf as follows:
Code included in http block
map $request_uri $bad_uri {
default 0;
"~*(\,|\)\+|/\,/|\{0\}|\(/\(|\.\.\.|\+\+\+|\||\\\"\\\")" 1;
}
Code included in server block
if ($bad_uri) {
return 403;
}
As far as I know both Apache and Nginx use perl regex so no change should be required when converting from former to the latter. However, following URI is giving 403 on Nginx but working fine on Apache:
www.example.com/some,url,with,commas
www.example.com/?q=some,url,with,commas
Finally found the issue.
In Apache RedirectMatch matches only the url without query string whereas $request_uri in nginx maps to url with query string.
So the correct code for Nginx is:
map $uri $bad_uri {
default 0;
"~*(\,|\)\+|/\,/|\{0\}|\(/\(|\.\.\.|\+\+\+|\||\\\"\\\")" 1;
}
I am trying to run a domain and sub-domain in the same port using Nginx, and do not have success yet.
I have a domain named www.just4bettors.mobi which is for a mobile page, and the subdomain must be named www.desktop.just4bettors.mobi which is clearly for the desktop site.
If you enter www.just4bettors.mobi everything works, you reach the page, but if you enter www.desktop.just4bettors.mobi you get This web page is not available. This is the server block I have so far
server {
large_client_header_buffers 1 1K;
listen 80;
server_name ~^(?<subdomain>[^.]*)\.?just4bettors.mobi$ just4bettors.mobi;
root /home/c0pt/capilleira/capilleiraclickandgamblemobile/www;
location / {
if ($subdomain) {
root /home/c0pt/capilleira/capilleiraclickandgambleweb/dist;
}
if ($host = "just4bettors.mobi") {
root /home/c0pt/capilleira/capilleiraclickandgamblemobile/www;
}
index index.html index.htm;
...
}
}
once I try to access to desktop.just4bettors.mobi, the console returns this GET http://desktop.just4bettors.mobi/ net::ERR_NAME_NOT_RESOLVED
you have that the roots are different here, mobile and web lives in separate places.
so, what am I missing here ?
I've managed Nginx configurations where millions of different domains were in use. It's very complicated trying to work inside one server{} block directive. Here is what I would do:
server {
large_client_header_buffers 1 1K;
listen 80;
server_name www.just4bettors.mobi;
root /home/c0pt/capilleira/capilleiraclickandgamblemobile/www;
location / {
...
}
}
server {
large_client_header_buffers 1 1K;
listen 80;
server_name www.desktop.just4bettors.mob;
root /home/c0pt/capilleira/capilleiraclickandgambleweb/dist;
location / {
...
}
}
You can read more about this here: http://wiki.nginx.org/Pitfalls#Server_Name