【问题标题】:Nginx: running wordpress in subfolder of a Symfony2 appNginx:在 Symfony2 应用程序的子文件夹中运行 wordpress
【发布时间】:2017-11-14 20:05:33
【问题描述】:

我使用 nginx 运行 Symfony2 应用程序,并希望将 wordpress 安装集成到公共网络文件夹的子文件夹中。

例子:

http://www.example.com          -> Symfony 2
http://www.example.com/magazin  -> Wordpress

通过 Symfony 应用程序的原始 nginx 配置,我可以成功地向 wordpress 的起始页以及整个管理区域(包括插件安装等)发出请求。

但是由于我将 wordpress 配置为对帖子使用自定义 url 方案“年/月/标题”,因此请求以 404 结束。我发现,不是 wordpress 是收到请求的应用程序,而是 symfony ,这当然不知道在这里做什么。 wordpress 为帖子创建的 URL 是正确的(例如 http://www.example.com/magazin/2015/12/my-interesing-post)。

是否可以扩展 nginx 配置以处理特定文件夹“/magazin/”下的所有请求,如果可以,如何?

这是我的 nginx 配置,目前只处理 Symfony2 应用程序:

server {
  listen *:80;
  server_name           www.example.de;


  index  app.php index.php index.html;

  access_log            /var/log/nginx/www.example.de.access.log combined;
  error_log             /var/log/nginx/www.example.de.error.log;

  location ~ \.php$ {

    root          /data/www/www.example.de/current/web;
    include       /etc/nginx/fastcgi_params;
    try_files     $uri $uri/ /app.php?$query_string;

    fastcgi_pass  unix:/var/run/php5-fpm.sock;
    fastcgi_index app_prod.php;
    fastcgi_param X_FORWARD_PORT "80";
    fastcgi_param CUSTOMER_ENV customer_default;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    include fastcgi_params;
  }

  location / {

    root      /data/www/www.example.de/current/web;
    index     app.php index.php index.html;
    try_files $uri $uri/ /app.php?$query_string;
  }
}

【问题讨论】:

    标签: php wordpress symfony nginx


    【解决方案1】:

    扩展为 malcolms 解释,这应该可以完成工作:

    (如果你的日志说默认的 nginx 目录的路径是前置的,你只需要重新定义根目录)

    location /magazin {
        root      /data/www/www.example.de/current/web;
        index     index.php;
        try_files $uri $uri/ /magazin/index.php?q=$uri;
    }
    

    此外,我不太确定,但我建议将此位置块插入任何其他可以获取此路线的位置块之前(位置 /magazin 和之后的位置 /)。

    【讨论】:

      【解决方案2】:

      如果你用的是symfony2和php7,你可以试试这个配置:

      server {
        listen    *:80;
        server_name www.example.com;
        root /var/www/example.com/web/;
        index index.php index.html index.htm;
        access_log off;
      
        location @rewriteapp {
             rewrite ^(.*)$ /app.php/$1 last;
        }
      
        location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
             expires max;
             try_files $uri =404;
        }
      
        location / {
            index app.php;
            try_files $uri @rewriteapp;
        }
      
        # BLOG AREA START
        location @rewriteblog {
             rewrite ^(.*)$ /blog/index.php?q=$uri&$args;
        }
      
        location @rewriteblogadmin {
             rewrite ^(.*)$ /blog/wp-admin/index.php?q=$uri&$args;
        }
      
        location = /blog/favicon.ico {
            log_not_found off;
            access_log off;
        }
      
        location = /blog/robots.txt {
            allow all;
            log_not_found off;
            access_log off;
        }
      
        location /blog {
            # This is cool because no php is touched for static content.
            # include the "?$args" part so non-default permalinks doesn't break when using query string
            try_files $uri @rewriteblog;
        }
      
        location /blog/wp-admin {
            # This is cool because no php is touched for static content.
            # include the "?$args" part so non-default permalinks doesn't break when using query string
            try_files $uri @rewriteblogadmin;
        }
      
        # BLOG
        location ~ ^/(blog|blog\/wp-admin)/(.*)\.php(/|$) {
            try_files $uri =404;
            fastcgi_index index.php;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            include fastcgi_params;
      
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT $realpath_root;
      
            fastcgi_intercept_errors on;
            fastcgi_buffers 16 16k;
            fastcgi_buffer_size 32k;
        }
      
        # PROD
        # This rule should only be placed on your development environment
        # In production, don't include this and don't deploy app_dev.php or config.php
        location ~ ^/(app|config)\.php(/|$) {
            fastcgi_index app.php;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT $realpath_root;
        }
      
        # return 404 for all other php files not matching the front controller
        # this prevents access to other php files you don't want to be accessible.
        location ~ \.php$ {
              return 404;
        }
      
        error_log /var/log/nginx/examplecom_error.log;
        access_log /var/log/nginx/examplecom_access.log;
      }
      

      【讨论】:

        【解决方案3】:

        您可以在子文件夹中添加location

        location /magazin {
            index index.php;
            try_files $uri $uri/ /magazin/index.php?q=$uri;
        }
        

        【讨论】:

        • 不幸的是,这会将所有请求 /magazin/* 以及所有 .css/.js/.jpg-Requests 路由到 index.php,现在以 404 结束
        • 尝试把最后一行改成:try_files $uri $uri/ /magazin/index.php;
        • 没有帮助。即使可以直接在服务器上确保路径在文件系统上有效,也不会访问静态文件
        • 我现在不在 nginx 上,所以无法检查,但还有一件事:在杂志后添加斜杠:location /magazin/ {
        猜你喜欢
        • 2019-12-22
        • 1970-01-01
        • 2011-03-23
        • 2014-03-04
        • 1970-01-01
        • 2016-12-29
        • 2015-09-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多