NGINX 反向代理子域从 url 部分剥离端口

NGINX Reverse proxy subdomain to part strips port from url

我在端口 2000 上安装了一个 NodeJS 应用程序 运行,在端口 2010 上安装了一个 Wordpress。

我已尝试设置我的 nginx.conf 文件来执行以下操作:

我已经设置了 info 子域的 DNS 设置,因此它指向与我的 NodeJS 应用程序相同的服务器。

NodeJS 应用程序运行良好。不过 Wordpress 安装确实很奇怪。

Wordpress 网址一直都是错误的。我希望它们看起来像 http://info.domain.com/blah.../ but instead they look like this http://domain.com:2010/blah.../ — 信息。domain.com 似乎只是重定向。

我尝试修改 Wordpress 设置,看看它们是否与上述问题有关。以前的Wordpress地址和站点地址都是http://domain.com:2010. I tried changing them to http://info.domain.com。这不起作用,因为 "info" 部分似乎从我的 URL 中删除导致错误。为了回到以前的情况,我使用 phpMyAdmin 将它们改回。

现在我遇到了一个全新的问题:http://domain.com:2010/somepage/ works as expected but when I try to access Wordpress' home page, either through http://domain.com:2010/ or through http://info.domain.com/ I am redirected to http://domain.com/ 和那里的 NodeJS 应用程序。

如果有人能帮助我了解问题所在,我将不胜感激。

我是服务器配置方面的初学者,我确信我的 nginx.conf 文件一团糟。我附在下面。

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

    # server for reverse proxy on node application on port 2000
    server {
        listen          80;
        server_name     domain.com www.domain.com;

        #location /moodle {
        #   proxy_pass http://moodle.domain.com; 
        #   proxy_http_version 1.1;
        #   proxy_set_header Upgrade $http_upgrade;
        #   proxy_set_header Connection 'upgrade';
        #   proxy_set_header Host $host;
        #   proxy_cache_bypass $http_upgrade;
        #}

        location /old {
            rewrite ^ http://old.domain.com/ permanent;
        }

        location /moodle {
            rewrite ^ http://moodle.domain.com/ permanent;
        }

        location / {
            proxy_pass http://domain.com:2000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }

        location /info {
            proxy_pass http://domain.com:2010;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }

    # server for reverse proxy on node application on port 3000
    server {
        listen          80;
        server_name     beta.domain.com;

        location / {
            proxy_pass http://beta.domain.com:2000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }

    # Test re-routing to info.domain.com
    server {
        listen 80;
        server_name     info.domain.com;

        location / {
            proxy_pass http://domain.com:2010/;
        }
    }

    # server access for phpmyadmin to listen on port 81
    server {
        listen              81;
        server_name         localhost;
        root                /usr/share/phpmyadmin;
        index               index.php index.html index.htm;

        if (!-e $request_filename) {
            rewrite ^/(.+)$ /index.html?url= last;
            break;
        }

        location ~ .php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include /etc/nginx/fastcgi_params;
        }
   }

   # server access for Wordpress to listen on port 2010
    server {
        listen              2010;
        server_name         info.domain.com;
        root                /home/take/wordpress;
        index               index.php index.html index.htm;
        client_max_body_size 256M;

        #if (!-e $request_filename) {
        #   rewrite ^/(.+)$ /index.html?url= last;
        #   break;
        #}

        #location ~ .php$ {
        #   try_files $uri =404;
        #   fastcgi_pass unix:/var/run/php5-fpm.sock;
        #   fastcgi_index index.php;
        #   include /etc/nginx/fastcgi_params;
        #}



        # WordPress single site rules.
        # Designed to be included in any server {} block.

        # This order might seem weird - this is attempted to match last if rules below fail.
        # http://wiki.nginx.org/HttpCoreModule
        location / {
            try_files $uri $uri/ /index.php?$args;
        }

        # Add trailing slash to */wp-admin requests.
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;

        # Directives to send expires headers and turn off 404 error logging.
        location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
               access_log off; log_not_found off; expires max;
        }

        # Uncomment one of the lines below for the appropriate caching plugin (if used).
        #include global/wordpress-wp-super-cache.conf;
        #include global/wordpress-w3-total-cache.conf;

        # Pass all .php files onto a php-fpm/php-fcgi server.
        location ~ [^/]\.php(/|$) {
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            if (!-f $document_root$fastcgi_script_name) {
                return 404;
            }
            # This is a robust solution for path info security issue and works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default)

            include fastcgi_params;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #   fastcgi_intercept_errors on;
        #   fastcgi_pass php;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
        }

   }




}

info.domain.com 重定向您的原因是因为您将其代理到 domain.com。删除 # Test re-routing to info.domain.com.

您的 info.domain.com 块应如下所示(listen 80; 是更改):

# server access for Wordpress to listen on port 80
server {
    listen              80;
    server_name         info.domain.com;
    root                /home/take/wordpress;
    index               index.php index.html index.htm;
    client_max_body_size 256M;

    #if (!-e $request_filename) {
    #   rewrite ^/(.+)$ /index.html?url= last;
    #   break;
    #}

    #location ~ .php$ {
    #   try_files $uri =404;
    #   fastcgi_pass unix:/var/run/php5-fpm.sock;
    #   fastcgi_index index.php;
    #   include /etc/nginx/fastcgi_params;
    #}



    # WordPress single site rules.
    # Designed to be included in any server {} block.

    # This order might seem weird - this is attempted to match last if rules below fail.
    # http://wiki.nginx.org/HttpCoreModule
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # Add trailing slash to */wp-admin requests.
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

    # Directives to send expires headers and turn off 404 error logging.
    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
           access_log off; log_not_found off; expires max;
    }

    # Uncomment one of the lines below for the appropriate caching plugin (if used).
    #include global/wordpress-wp-super-cache.conf;
    #include global/wordpress-w3-total-cache.conf;

    # Pass all .php files onto a php-fpm/php-fcgi server.
    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
        }
        # This is a robust solution for path info security issue and works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default)

        include fastcgi_params;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #   fastcgi_intercept_errors on;
    #   fastcgi_pass php;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}

然后,当您访问 domain.com/info 时,使用重定向而不是代理请求。将 /info 位置块更改为:

location /info {
    return 301 $scheme://info.domain.com$request_uri;
}

解释:

通常,当您 proxy_pass 您告诉 Nginx 充当网关接口(例如 Gunicorn 或 uWSGI 或 fastCGI 等)的反向代理。你所做的是要求 Nginx 绑定到你正在代理的同一端口,这在理论上可行但不实用。

您的 Wordpress 站点绑定到 unix 套接字,因此为了访问它,您需要通过 Nginx。当您在浏览器中发出请求时,例如 http://info.domain.com,它默认为端口 80。我假设您理解这一点,因为您已将此请求代理到端口 2010,但您应该直接从这个街区。

我希望所有这些都有意义。