重写显示的URL,而无需实际重定向

时间:2018-07-28 14:37:33

标签: nginx nginx-location nginx-reverse-proxy

我正尝试更改显示的url,以保持所有相对于主域的子域url,例如 maindomain.com/some_page 将从 sub.maindomain.com/some_page提供,但要将显示的URL在浏览器中保留为 maindomain.com/some_page 。 考虑到主域和子域托管在不同的服务器上,如下所述:

  1. maindomain.com是由LEMP服务器支持的wordpress,具有以下nginx.conf:

    server {
    listen 80;
    server_name maindomain.com;
    root /var/www/html;
    index index.php index.html index.htm;
    include /var/www/html/nginx.conf;
    client_max_body_size 65M;
    
    error_page 404 @sub;
    
    location / {
            try_files $uri $uri/ /index.php;
    }
    
    location /xmlrpc.php {
            deny all;
    }
    
    location = /xmlrpc.php {
            deny all;
            access_log off;
            log_not_found off;
            return 444;
    }
    
    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_intercept_errors on;
    }
    
    location ~* ^.+\.(?:css|cur|js|jpe?g|gif|htc|bmp|ico|rtf|png|xml|otf|ttf|eot|woff|mid|midi|woff2|svg|png|ico|zip|tgz|gz|rar|bz2)$ {
            expires max;
            log_not_found off;
    }
    
    # redirect all not found pages to the sub domain
    location @sub {
            rewrite ^ http://sub.maindomain.com$request_uri redirect;
    }
    }
    
  2. sub.maindomain.com是使用Nginx作为代理服务器的Node.js应用。

    upstream app {
      server 127.0.0.1:9000 fail_timeout=120;
    }
    
    server {
      listen 80;
      server_name sub.maindomain.com;
      root /var/webapp;
      # Compression
      gzip on;
      gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
      gzip_buffers 16 8k;
      gzip_disable "MSIE [1-6]\.";
      # Error pages
      error_page 404 /404.html;
      location /404.html { root /app/public; }
      error_page 500 /500.html;
      location /500.html { root /app/public; }
      # Client configuration
      client_max_body_size 4G;
      keepalive_timeout 120;
    
      # Logs
      access_log no;
    
      # Serve static assets
      location ^~ /assets/ {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
      }
    
      # Try physical files first then proxy to running application
      try_files $uri/index.html $uri @app;
      location @app {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Url-Scheme $scheme;
        proxy_redirect off;
        rewrite ^/(.*) http://maindomain.com$1 break;
        proxy_pass http://app;
      }
    }
    

我尝试了各种不同的重写规则,但是没有办法解决问题。

0 个答案:

没有答案