Nginx reverse proxy and subdomains
Solution 1
You may want to check your DNS setup.
I also use multiple subdomains which I create on the fly for various applications using web frameworks such as Python Django or Ruby on Rails.
A typical example is at mydomain.com
I may want to have myapp.mydomain.com
where myapp is a framework served at my_server_IP:some_port
.
In order to achieve such thing (many subdomains on the fly at the same OR different ports) I delegate the decision from the DNS to the Web server with a Wildcard DNS record see Wikipedia. As the name suggests, such record produces a catch all domain which can be easily managed from the web server with the use of virtual hosts - domain proxys, etc.
An A
wildcard record at GoDaddy's DNS management tool looks like this:
Host Points To TTL
* YOUR_SERVER_IP 1 Hour
And an Nginx configuration file which passes all requests for app.mydomain.com
to otherserver.com:9000/index.html
The result: you type in browser http://app.mydomain.com
and Nginx serves content from otherserver.com:9000/index.html
which can be another server or application, etc
server {
listen 80;
server_name app.mydomain.com www.app.mydomain.com;
access_log /var/log/nginx/app_mydomain_com_access.log;
error_log /var/log/nginx/pp_mydomain_com_error.log;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location / {
proxy_pass http://otherserver.com:9000/index.html;
}
}
Solution 2
DNS were all rights, I was doing some trial in local domain before putting them on production.. I solved the issue more easier (I don't have index.html or any other files, I have only folder name).. Finally, I think this would be helpful for someone, the configuration is:
upstream tomcat_server {
server tomcat.domain.com:8080;
}
server {
listen 80;
server_name app1.domain.com;
location / {
proxy_pass http://tomcat_server/app1/;
sub_filter /app1/ /;
}
}
The " /" at the end of proxy_pass is necessary ...
sub_filter is needed to avoid that the result would be something like
app1.domain.com/app1
That's all.. I'm trying to improve this configuration for app working with tomcat 6, because the use of subdomains could generate (as in my case) some disease in the apps.
Related videos on Youtube
Luciana D
Updated on September 18, 2022Comments
-
Luciana D over 1 year
I have some problem with Nginx configuration. I've already searched for an issue but the proposed solutions I saw didn't work for me.
I have some apps on a tomcat server and some apps on a jboss server. I configured on another server Nginx as reverse proxy and it works properly. Now, what I would do is to have access to the apps on tomcat and jboss using domain names. Eg. I have apps on tomcat onto this address: tomcat.domain.com:8080/app1 ; tomcat.domain.com:8080/app2 and so on, even for jboss apps.. With reverse proxy I have this output: proxy.domain.com/app1 proxy.domain.com/app2 and so on, so I don't have to specify neither the port number not the belonging to a specified server. But I would use subdomains in this way: app1.domain.com, app2.domain.com and so on. So, my configuration of Nginx , the one that works and used just for reverse proxy is that (i will report the lines about tomcat only, for simplicity):
upstream tomcat_server { server tomcat.domain.com:8080; } server { listen 80; location /app1 { proxy_pass http://tomcat_server; proxy_set_header X-Forwarded-Host $host; } }
For subdomain configuration I modified the part about server definition like this:
server { listen 80; server_name app1.domain.com; location / { proxy_pass http://tomcat_server/app1; proxy_set_header X-Forwarded-Host $host; } }
as suggested in some forums , but it doesn't work. I specify that, on the server that manage DNS app1.domain.com points to the proxy.domain.com server. I saw logs, but there wasn't any. What should I do?
Thanks
Luciana