nginx - rewrite domain.com:port to sub.domain.com

僤鯓⒐⒋嵵緔 提交于 2019-12-03 20:48:00

If you actually want to redirect (301 or 302) your web traffic

You create a server {} section listening on port 3000 and you just redirect it to another server {} section that is listening on port 80. In each server {} section set the listen property appropriately. I guess you are trying to handle the redirection within à single server section and according to this page the listen directive applies to a server context

If you want to use nginx as a proxy

Then what you are looking for is the proxy_pass directive. Here is a sample configuration extracted from an config I have to use nginx as a proxy for my rails app (thin). Basically my app runs locally (but it would also work on a remote host) on port 3200 and the relevant nginx config part looks as follow:

  upstream my-app-cluster
  {
      server localhost:3200;
  }  
  server
  {
    listen       80;
    server_name mydomain.com;

    root /root/to/public/folder;

    access_log  /my/app/log/folder/myapp.log;

    location / {
      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_redirect off;

      if (-f $request_filename/index.html) {
        rewrite (.*) $1/index.html break;
      }
      if (-f $request_filename.html) {
        rewrite (.*) $1.html break;
      }
      if (!-f $request_filename) {
        proxy_pass http://my-app-cluster;
        break;
      }
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   html;
    }

  }

You could use Passenger in nginx to delivery the Ruby app - that's the method we are currently using.

http://www.modrails.com/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!