NGINX反向代理的子域从URL

NGINX反向代理的子域从URL

问题描述:

部分带口我有一个应用程序的NodeJS在端口2000上运行和2010年NGINX反向代理的子域从URL

我试图建立我的nginx.conf文件做以下端口一个WordPress安装:

  • domain.com:服务从端口应用的NodeJS 2000
  • info.domain.com:服务WordPress的内容

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

NodeJS应用程序工作正常。 WordPress的安装行为真的很奇怪。

Wordpress网址一直都是错误的。我希望他们看起来像http://info.domain.com/blah.../,但他们看起来像这样http://domain.com:2010/blah.../ - info.domain.com只是似乎重定向。

我试着搞乱Wordpress设置,看看他们是否有上述问题的一些部分。以前的WordPress的地址和网站地址都是http://domain.com:2010。我试图将它们更改为http://info.domain.com。这不起作用,因为“信息”部分似乎从我的URL导致错误中被删除。回到之前的情况,我使用phpMyAdmin将它们改回。

现在我有一个全新的问题:http://domain.com:2010/somepage/按预期工作,但是当我试图访问WordPress的主页,无论是通过http://domain.com:2010/或通过http://info.domain.com/我重定向到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=$1 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=$1 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=$1 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或快速等东西)。你做了什么,被要求Nginx绑定到你正在代理的同一个端口,这在理论上是可行的,但它不实际。

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

我希望所有这些都有道理。

+0

感谢您的回复。这很有道理。但是,我的info.domain.com仍然被重定向到domain.com网站。不过,指向特定文件的网址可以正常工作(info.domain.com/someFile.php正常,info.domain.com/someSubdir/不会)。你有什么建议,为什么发生这种情况,我可以做些什么呢? – Regicollis

+1

这很奇怪,它不应该重定向。您是否重新启动了Nginx并清除了您可能拥有的任何缓存机制?如果有,请发布您的更新配置。当你说'info.domain.com/someSubdir /'不工作,你得到的错误是什么,你期望的结果是什么?一般来说,Nginx不提供目录,只提供文件。如果你设置'autoindex on',你会得到一个可以浏览的分层结构,但是这不推荐。 –

+0

我尝试了一段时间,然后工作。这一定是一些关键问题。一切正常,现在应该如此。非常感谢您的回答。 – Regicollis