Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a web app running on a nginx server on local ip 192.168.0.30:80

I have this in my etc/hosts

127.0.0.1 w.myapp.in

If someone accesses my app using a "w" subdomain, it shows a webdav interface, otherwise it runs normally

(for example, someone calls http://myapp.in , it goes into the app, and http://w.myapp.in goes into webdav interface - this is done within the app, nginx has nothing to do with it)

Because I don't have a dns or anything like that, users must access the app by ip. A problem appears if someone wants to access the webdav interface, because you cannot access the app by a subdomain - unless you write a line in your local hosts file, which is not a solution)

A possible solution

If it's possible to setup the nginx server so that if someone calls http://192.168.0.30 (on port 80), it goes normally into the app, but if a user tries to access say http://192.168.0.30:81 (another defined port) it redirects internally to w.myapp.in, and the app sees the subdomain

Given the app, can this be done? If yes, what should I put in the nginx config file? And if you guys think of a better solution, I'm open to any.

share|improve this question

2 Answers 2

Yes, you can get nginx to listen on two ports and proxy requests on different port numbers appropriately. Something like this:

server {
    listen 80;
    proxy_pass http://localhost:8080/myapp/;
}

server {
    listen 81;
    proxy_pass http://localhost:8080/webdav/;
}
share|improve this answer

I would try the rewrite directive. Have one server listening on port 80, and optionally rewriting to a real server name. And another server listening on port 81, and rewriting to the 'w' subdomain. Not 100% sure this will work, but it's worth a shot.

server {
  listen 80;
  rewrite ^ http://example.com$request_uri? permanent;
  [...]
}

server {
  listen 81;
  rewrite ^ http://w.example.com$request_uri? permanent;
  [...]
}

http://wiki.nginx.org/HttpRewriteModule

share|improve this answer
    
The users don't necessarily have working DNS, so sending a redirect to a domain name isn't going to work. –  mgorven Apr 19 '12 at 17:33
    
Sorry, I didn't realize that was a redirect. I intended it to just be a rewrite, but apparently the nginx rewrite module isn't able to rewrite host headers without a redirect. However, there is a post that addresses that issue: stackoverflow.com/questions/9110020/wildcard-nginx-rewrite –  mltsy Jun 14 '12 at 16:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.