Nginx:Wordpress作为Rails站点上的子目录错误地路由 [英] Nginx: Wordpress as subdirectory on Rails site routes incorrectly

查看:91
本文介绍了Nginx:Wordpress作为Rails站点上的子目录错误地路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我导航到不以.php结尾的URL时,我遇到了来自wordpress/nginx的令人沮丧的重定向.例如,在wordpress中更新某些内容后,我可能会重定向到:

I am experiencing a frustrating redirect from wordpress/nginx when I navigate to a URL that does not end in .php. For example after updating something in wordpress, I might be redirect to:

http://www.example.com/wp-admin/options-media.php?settings-updated=true # with blog/ ommited

代替

http://www.example.com/blog/wp-admin/options-media.php?settings-updated=true 

我真的很想知道为什么会这样,并且尝试了location ~ /blog.*行,但有很多变化,但没有成功.

I am really curious to know why this is happening, and have experimented with the location ~ /blog.* line with numerous variations to no success.

我的域和博客nginx conf文件在下面,对此我将非常感谢!

My domain and blog nginx conf files are below, I would really appreciate any help on this!

example.com.conf:

example.com.conf:

upstream blog {
   server 127.0.0.1:80;
}
server {
  listen 80;
  server_name www.example.com direct.example.com;
  root /home/webapps/example/live/current/public;

  error_log logs/example.com-error.log;
  access_log logs/example.com-access.log;

  passenger_enabled on;
  rails_env production;
  passenger_min_instances 3;
  passenger_spawn_method smart;
  passenger_use_global_queue on;

  location ~ /blog.* {
    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;

    gzip on;
    gzip_min_length  1100;
    gzip_buffers     4 8k;
    gzip_proxied any;
    gzip_types  text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    if (!-f $request_filename) {
      #rewrite ^/blog$     /;
      rewrite ^/blog/(.*)/$ /blog/$1;
      rewrite ^/blog/(.*)$ /$1;
      proxy_pass http://blog;
      break;
    }
  }
}

blog.conf:

blog.conf:

server {
    listen      127.0.0.1:80;
    server_name blog.example.com;

    error_log logs/blog-error.log;
    access_log logs/blog-access.log;

    location / {
        root   /home/blog;
        index  index.php index.html index.htm;

        # this serves static files that exist without running other rewrite tests
        if (-f $request_filename) {
            expires 30d;
            break;
        }

        # this sends all non-existing file or directory requests to index.php
        if (!-e $request_filename) {
            rewrite ^(.+)$ /index.php?q=$1 last;
        }
    }

    location ~ \.php$ {
        fastcgi_pass   localhost:9000;  # port where FastCGI processes were spawned
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME    /home/blog$fastcgi_script_name;  # same path as above

        fastcgi_param  QUERY_STRING       $query_string;
        fastcgi_param  REQUEST_METHOD     $request_method;
        fastcgi_param  CONTENT_TYPE       $content_type;
        fastcgi_param  CONTENT_LENGTH     $content_length;

        fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
        fastcgi_param  REQUEST_URI        $request_uri;
        fastcgi_param  DOCUMENT_URI       $document_uri;
        fastcgi_param  DOCUMENT_ROOT      $document_root;
        fastcgi_param  SERVER_PROTOCOL    $server_protocol;

        fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
        fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

        fastcgi_param  REMOTE_ADDR        $remote_addr;
        fastcgi_param  REMOTE_PORT        $remote_port;
        fastcgi_param  SERVER_ADDR        $server_addr;
        fastcgi_param  SERVER_PORT        $server_port;
        fastcgi_param  SERVER_NAME        $server_name;

        # required if PHP was built with --enable-force-cgi-redirect
        # required if PHP was built with --enable-force-cgi-redirect
        fastcgi_param  REDIRECT_STATUS    200;
    }
}

推荐答案

我在某一时刻遇到了类似的问题,结果发现我在Wordpress MySQL数据库中的配置不正确,这是由于我将博客从/中移出了root/到/root/blog/.

I had a similar problem at one point and it turned out that my configuration in the Wordpress MySQL Database was incorrect, this was due to me moving the blog from /root/ to /root/blog/.

这就是我所做的更改:

使用PHPMyAdmin或其他任何方式登录到WP DB.

Login to the WP DB using PHPMyAdmin or whatever.

wp_options表下,检查以下内容:

Under wp_options table check the following:

siteurl    : should be -> http://example.com/blog
home       : should be -> http://example.com/blog
remote_url : should be -> http://example.com/blog/

注意斜杠.

这篇关于Nginx:Wordpress作为Rails站点上的子目录错误地路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆