本文介绍了nginx-将domain.com:port重写为sub.domain.com的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何将带有端口的域重写为子域?

How can i rewrite a domain with a port to a subdomain?

e.q .: domain.com:3000sub.domain.com吗?

e.q.: domain.com:3000 to sub.domain.com ?

感谢您的帮助! :)

greetz

推荐答案

如果您确实要重定向(301或302)您的网络流量

您创建一个侦听端口3000的server {}部分,然后将其重定向到另一个侦听端口80的server {}部分.在每个server {}部分中,适当地设置listen属性.我猜您正在尝试在单个server部分并根据此页面处理重定向listen指令适用于server上下文

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

然后,您要查找的是proxy_pass指令.这是从配置中提取的示例配置,我必须使用nginx作为我的rails应用程序(薄)的代理.基本上,我的应用程序在端口3200上本地运行(但它也可以在远程主机上运行),相关的nginx配置部分如下所示:

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;
    }

  }

这篇关于nginx-将domain.com:port重写为sub.domain.com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 07:09