【问题标题】:How to configure correctly to nginx and apache for zendframework2如何为zendframework2正确配置nginx和apache
【发布时间】:2014-12-21 05:04:07
【问题描述】:

环境

CentOS 6.5 x86_64 PHP 5.6.3 httpd 2.4.10 Nginx 1.6.2 ZendFramework2.3.3

目标

安装 ZF2 骨架应用程序

现在

我将 nginx 配置为将 php 文件传递​​给 apache。我在 nginx 或 apache 中将 zf2 框架应用程序配置为虚拟主机。我手动安装 zendframework2.3.3。我可以通过127.0.0.1:38082(httpd配置)正常访问网站。但是,当我通过 127.0.0.1:8082(由 nginx 配置)访问网站时,出现如下 404 错误。任何人都可以给我一些建议吗?谢谢。

我的配置

Nginx 虚拟主机

server {
    listen       8082;
    server_name  www.eg.m;

    #charset koi8-r;

    access_log  logs/eg.access.log  main;

    location / {
        root   /data/webapp/www/zf-app/public;
        index  index.php index.html index.htm Default.html Default.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    location ~ \.php$ {
        proxy_pass   http://127.0.0.1:38082;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}

}

httpd 虚拟主机

Listen 38082

<VirtualHost *:38082>
    ServerAdmin w@gmail.com
    DocumentRoot "/data/webapp/www/zf-app/public"
    ServerName www.eg.m
    ServerAlias eg.m
    ErrorLog "logs/eg.m-error_log"
    CustomLog "logs/eg.m-access_log" common

    SetEnv ZF2_PATH "/data/webapp/www/ZendFramework-2.3.3/library"
    SetEnv APPLICATION_ENV "development"

    <Directory /data/webapp/www/zf-app/public>
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all

    </Directory>

</VirtualHost>

我出错了

A 404 error occurred
Page not found.

The requested URL could not be matched by routing.
No Exception available

日志

apache 使用 127.0.0.1:38082 直接从 apache 记录日志

错误日志

[Mon Dec 22 19:52:37.100637 2014] [authz_core:debug] [pid 17524:tid 139805499234048] mod_authz_core.c(802): [client 127.0.0.1:38934] AH01626: authorization result of Require all granted: granted
[Mon Dec 22 19:52:37.100694 2014] [authz_core:debug] [pid 17524:tid 139805499234048] mod_authz_core.c(802): [client 127.0.0.1:38934] AH01626: authorization result of <RequireAny>: granted
[Mon Dec 22 19:52:37.184903 2014] [authz_core:debug] [pid 17524:tid 139805490841344] mod_authz_core.c(802): [client 127.0.0.1:38934] AH01626: authorization result of Require all granted: granted, referer: http://127.0.0.1:38082/
[Mon Dec 22 19:52:37.184927 2014] [authz_core:debug] [pid 17524:tid 139805490841344] mod_authz_core.c(802): [client 127.0.0.1:38934] AH01626: authorization result of <RequireAny>: granted, referer: http://127.0.0.1:38082/
[Mon Dec 22 19:52:37.186510 2014] [authz_core:debug] [pid 17519:tid 139805499234048] mod_authz_core.c(802): [client 127.0.0.1:38935] AH01626: authorization result of Require all granted: granted, referer: http://127.0.0.1:38082/
[Mon Dec 22 19:52:37.186549 2014] [authz_core:debug] [pid 17519:tid 139805499234048] mod_authz_core.c(802): [client 127.0.0.1:38935] AH01626: authorization result of <RequireAny>: granted, referer: http://127.0.0.1:38082/
[Mon Dec 22 19:52:37.187952 2014] [authz_core:debug] [pid 17524:tid 139805413340928] mod_authz_core.c(802): [client 127.0.0.1:38934] AH01626: authorization result of Require all granted: granted, referer: http://127.0.0.1:38082/
[Mon Dec 22 19:52:37.187977 2014] [authz_core:debug] [pid 17524:tid 139805413340928] mod_authz_core.c(802): [client 127.0.0.1:38934] AH01626: authorization result of <RequireAny>: granted, referer: http://127.0.0.1:38082/
[Mon Dec 22 19:52:37.189887 2014] [authz_core:debug] [pid 17524:tid 139805404948224] mod_authz_core.c(802): [client 127.0.0.1:38934] AH01626: authorization result of Require all granted: granted, referer: http://127.0.0.1:38082/
[Mon Dec 22 19:52:37.189901 2014] [authz_core:debug] [pid 17524:tid 139805404948224] mod_authz_core.c(802): [client 127.0.0.1:38934] AH01626: authorization result of <RequireAny>: granted, referer: http://127.0.0.1:38082/
[Mon Dec 22 19:52:37.191484 2014] [authz_core:debug] [pid 17519:tid 139805490841344] mod_authz_core.c(802): [client 127.0.0.1:38935] AH01626: authorization result of Require all granted: granted, referer: http://127.0.0.1:38082/
[Mon Dec 22 19:52:37.191502 2014] [authz_core:debug] [pid 17519:tid 139805490841344] mod_authz_core.c(802): [client 127.0.0.1:38935] AH01626: authorization result of <RequireAny>: granted, referer: http://127.0.0.1:38082/
[Mon Dec 22 19:52:37.242167 2014] [authz_core:debug] [pid 17524:tid 139805396555520] mod_authz_core.c(802): [client 127.0.0.1:38934] AH01626: authorization result of Require all granted: granted, referer: http://127.0.0.1:38082/
[Mon Dec 22 19:52:37.242193 2014] [authz_core:debug] [pid 17524:tid 139805396555520] mod_authz_core.c(802): [client 127.0.0.1:38934] AH01626: authorization result of <RequireAny>: granted, referer: http://127.0.0.1:38082/

访问日志

127.0.0.1 - - [22/Dec/2014:19:52:37 +0800] "GET / HTTP/1.1" 200 5339
127.0.0.1 - - [22/Dec/2014:19:52:37 +0800] "GET /css/bootstrap.min.css HTTP/1.1" 200 99548
127.0.0.1 - - [22/Dec/2014:19:52:37 +0800] "GET /css/style.css HTTP/1.1" 200 1042
127.0.0.1 - - [22/Dec/2014:19:52:37 +0800] "GET /css/bootstrap-theme.min.css HTTP/1.1" 200 13135
127.0.0.1 - - [22/Dec/2014:19:52:37 +0800] "GET /js/jquery.min.js HTTP/1.1" 200 93107
127.0.0.1 - - [22/Dec/2014:19:52:37 +0800] "GET /js/bootstrap.min.js HTTP/1.1" 200 27822
127.0.0.1 - - [22/Dec/2014:19:52:37 +0800] "GET /img/zf2-logo.png HTTP/1.1" 200 738

从 nginx 访问时的 apache 日志

访问日志

127.0.0.1 - - [22/Dec/2014:20:29:08 +0800] "GET /index.php HTTP/1.0" 404 2345

错误日志

[Mon Dec 22 20:29:08.387773 2014] [authz_core:debug] [pid 20897:tid 140375947659008] mod_authz_core.c(802): [client 127.0.0.1:39363] AH01626: authorization result of Require all granted: granted
[Mon Dec 22 20:29:08.387818 2014] [authz_core:debug] [pid 20897:tid 140375947659008] mod_authz_core.c(802): [client 127.0.0.1:39363] AH01626: authorization result of <RequireAny>: granted

ngnix 访问日志。没有错误日志

127.0.0.1 - - [22/Dec/2014:20:29:08 +0800] "GET / HTTP/1.1" 404 2345 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0" "-"
127.0.0.1 - - [22/Dec/2014:20:29:08 +0800] "GET /css/bootstrap.min.css HTTP/1.1" 200 99548 "http://127.0.0.1:8082/" "Mozilla/5.0 (X11; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0" "-"
127.0.0.1 - - [22/Dec/2014:20:29:08 +0800] "GET /css/bootstrap-theme.min.css HTTP/1.1" 200 13135 "http://127.0.0.1:8082/" "Mozilla/5.0 (X11; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0" "-"
127.0.0.1 - - [22/Dec/2014:20:29:08 +0800] "GET /css/style.css HTTP/1.1" 200 1042 "http://127.0.0.1:8082/" "Mozilla/5.0 (X11; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0" "-"
127.0.0.1 - - [22/Dec/2014:20:29:08 +0800] "GET /js/jquery.min.js HTTP/1.1" 200 93107 "http://127.0.0.1:8082/" "Mozilla/5.0 (X11; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0" "-"
127.0.0.1 - - [22/Dec/2014:20:29:08 +0800] "GET /js/bootstrap.min.js HTTP/1.1" 200 27822 "http://127.0.0.1:8082/" "Mozilla/5.0 (X11; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0" "-"
127.0.0.1 - - [22/Dec/2014:20:29:08 +0800] "GET /img/zf2-logo.png HTTP/1.1" 200 738 "http://127.0.0.1:8082/" "Mozilla/5.0 (X11; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0" "-"

【问题讨论】:

  • 我超级累了,所以只是在这里吐痰,但是嗯......你可能需要在这个结尾的 $uri:proxy_pass http://127.0.0.1:38082$uri; 我也没有在这里阅读整面文字墙所以颗粒盐,但还是试试吧
  • 谢谢@Deryck 它不起作用。
  • 为什么要使用 2 个 Web 服务器?如果你需要 Nginx,你可以转储 Apache 并使用 php-fpm
  • 嗯,应该有办法在使用 zf2 @michaelbn 时将 nginx 和 apache 配置在一起。无论如何,谢谢您的考虑。
  • 您好,您成功了吗?如果可以,请分享经验

标签: php apache nginx zend-framework2


【解决方案1】:

我认为您缺少一些标题。尝试添加如下内容:

location ~ \.php$ {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_http_version 1.1;
    proxy_pass http://127.0.0.1:38082;
}

-- 更新--

我注意到日志之间的区别是http版本。我添加了一条规则来强制 1.1 版(检查)-希望它有效

【讨论】:

  • 还是不行。我可以确定 php 文件将传递给 apache 进行部署。实际上 index.php 是 apache 第一次部署的。但是部分页面似乎没有找到。
  • 是的,我可以看到它在路由上失败。当您从 Nginx 访问和直接从 Apache 访问时,显示 apache 日志中的相关行
  • 不适用于那个。第二次好像找不到/index.php
猜你喜欢
  • 2022-01-13
  • 2017-10-01
  • 2020-10-13
  • 2011-03-11
  • 2020-10-01
  • 2012-05-25
  • 2023-04-09
  • 1970-01-01
  • 2021-02-18
相关资源
最近更新 更多