rewrite defined and wildcard subdomain with different rules
May 29, 2013 09:51AM
Hi,

Got a pretty "complex" setup with rw rules and having issues to make it all work. Spent a lot of time on this and would appreciate some help.

These are the 5 rules I try to achieve. I can't combine them without something else failing. The example below have 3, 4 and 5.

1. defined domains
login.domian.com -> http://localhost:8090/login?domain=login
cms.domain.com -> http://localhost:8090/cms?domain=cms
up to 5 more like this.

2. special case to login subdomain with appended query.
user1.domain.com/login -> http://localhost:8090/login/?domain=user1

3. all other subomains are user related to specific defined pages. Ex project, dashboard, resources. This is actually the same as rule 1 but its a wildcard subdomain
user1.domain.com/project -> http://localhost:8090/project?domain=user1
user2.domain.com/dashboard -> http://localhost:8090/dashboard?domain= user2
endles subdomains.

4 specific paths that are pass through to proxy which are for posting data. for ex /api /authenticateUser /logout
user1.domain.com/api/events -> http://localhost:8090/events

5 static files. jpg|jpeg|gif|css|png|js|ico|html (just pass to proxy)
user1.domain.com/css/file.css -> http://localhost:8090/css/file.css

This is what I got so far.
upstream domain.com {
server 127.0.0.1:8090;
}
server {
listen 443;
ssl on;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
server_name "~^(?<name>.*)\.domain\.com$";
rewrite_log on;

access_log /var/www/default/logs/ssl_access.log;
error_log /var/www/default/logs/ssl_error.log debug;

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;

#5
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
proxy_pass http://localhost:8090;
proxy_redirect off;
}

#4
location /api {
proxy_pass http://localhost:8090;
proxy_redirect off;
}

#1 / 3
location / {
rewrite / $request_uri?domain=$name break;

proxy_pass http://localhost:8090;
proxy_redirect off;
}
}



Edited 1 time(s). Last edit at 05/29/2013 09:53AM by jonaseliasson.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 138
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready