nginx - 提供静态文件,调用 PHP

nginx - 提供静态文件,调用 PHP

我在将网站从 Apache 迁移到 nginx 时遇到了问题。大多数事情都运行正常,只是这里的一些静态文件出现了非常特殊的问题。

在服务器的文档根目录中安装了 phpBB,主题中的 .css 文件通过 PHP 解释器传输,而不是直接由 nginx 传送,我只是想不通为什么。

我的 nginx 站点配置:

server {

  listen   81; ## listen for ipv4
  listen   [::]:81 default ipv6only=on; ## listen for ipv6

  server_name  www.wanda.eu;

  access_log  /var/log/nginx/wanda.eu.access.log;
  error_log   /var/log/nginx/wanda.eu.error.log debug;

  root /srv/www/wanda.eu;

  location /static {
    try_files $uri $uri/ @php_index =404;
    expires 24h;
  }

  location / {
    #Secure arbitrary code execution on NON php files (ex: .png with PHP code)
    fastcgi_index index.php;
    try_files $uri $uri/ /index.php =404;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass unix:/tmp/php-cgi/php-cgi.socket;
    fastcgi_param SCRIPT_FILENAME /srv/www/wanda.eu$fastcgi_script_name;
  }
  location /dynamic {
    #Secure arbitrary code execution on NON php files (ex: .png with PHP code)
    try_files $uri $uri/ /dynamic/index.php =404;
    include /etc/nginx/fastcgi_params;
    fastcgi_index index.php;
    fastcgi_pass unix:/tmp/php-cgi/php-cgi.socket;
    fastcgi_param SCRIPT_FILENAME /srv/www/wanda.eu$fastcgi_script_name;
  }
}

(动态块是针对网站配置的特殊块,可生成图像 - 运行良好)

这里的问题是在请求 url /forum/styles/bastisstyle/theme/stylesheet.css 时

在 error_log 中启用调试后,我看到以下内容:

2012/08/08 16:24:20 [debug] 13885#0: *669 http request line: "GET /forum/styles/bastisstyle/theme/stylesheet.css HTTP/1.1"
2012/08/08 16:24:20 [debug] 13885#0: *669 http uri: "/forum/styles/bastisstyle/theme/stylesheet.css"
2012/08/08 16:24:20 [debug] 13885#0: *669 http args: ""
2012/08/08 16:24:20 [debug] 13885#0: *669 http exten: "css"
2012/08/08 16:24:20 [debug] 13885#0: *669 http process request header line
2012/08/08 16:24:20 [debug] 13885#0: *669 http header: "Host: www.wanda.eu:81"
2012/08/08 16:24:20 [debug] 13885#0: *669 http header: "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1"
2012/08/08 16:24:20 [debug] 13885#0: *669 http header: "Accept: text/css,*/*;q=0.1"
2012/08/08 16:24:20 [debug] 13885#0: *669 http header: "Accept-Language: en-us,en;q=0.5"
2012/08/08 16:24:20 [debug] 13885#0: *669 http header: "Accept-Encoding: gzip, deflate"
2012/08/08 16:24:20 [debug] 13885#0: *669 http header: "Connection: keep-alive"
2012/08/08 16:24:20 [debug] 13885#0: *669 http header: "Referer: http://www.wanda.eu:81/forum/"
2012/08/08 16:24:20 [debug] 13885#0: *669 http alloc large header buffer
2012/08/08 16:24:20 [debug] 13885#0: *669 malloc: 0000000001469B00:256
2012/08/08 16:24:20 [debug] 13885#0: *669 malloc: 0000000001497800:8192
2012/08/08 16:24:20 [debug] 13885#0: *669 http large header alloc: 0000000001497800 8192
2012/08/08 16:24:20 [debug] 13885#0: *669 http large header copy: 693
2012/08/08 16:24:20 [debug] 13885#0: *669 recv: fd:17 214 of 7499
2012/08/08 16:24:20 [debug] 13885#0: *669 http header: "Pragma: no-cache"
2012/08/08 16:24:20 [debug] 13885#0: *669 http header: "Cache-Control: no-cache"
2012/08/08 16:24:20 [debug] 13885#0: *669 http header done
2012/08/08 16:24:20 [debug] 13885#0: *669 event timer del: 17: 1344435925526
2012/08/08 16:24:20 [debug] 13885#0: *669 generic phase: 0
2012/08/08 16:24:20 [debug] 13885#0: *669 add cleanup: 00000000014972F8
2012/08/08 16:24:20 [debug] 13885#0: *669 generic phase: 1
2012/08/08 16:24:20 [debug] 13885#0: *669 test location: "/"
2012/08/08 16:24:20 [debug] 13885#0: *669 test location: "static"
2012/08/08 16:24:20 [debug] 13885#0: *669 test location: "dynamic"
2012/08/08 16:24:20 [debug] 13885#0: *669 using configuration "/"
2012/08/08 16:24:20 [debug] 13885#0: *669 http cl:-1 max:1048576
2012/08/08 16:24:20 [debug] 13885#0: *669 generic phase: 3
2012/08/08 16:24:20 [debug] 13885#0: *669 post rewrite phase: 4
2012/08/08 16:24:20 [debug] 13885#0: *669 generic phase: 5
2012/08/08 16:24:20 [debug] 13885#0: *669 add cleanup: 0000000001497330
2012/08/08 16:24:20 [debug] 13885#0: *669 generic phase: 6
2012/08/08 16:24:20 [debug] 13885#0: *669 generic phase: 7
2012/08/08 16:24:20 [debug] 13885#0: *669 access phase: 8
2012/08/08 16:24:20 [debug] 13885#0: *669 access phase: 9
2012/08/08 16:24:20 [debug] 13885#0: *669 post access phase: 10
2012/08/08 16:24:20 [debug] 13885#0: *669 try files phase: 11
2012/08/08 16:24:20 [debug] 13885#0: *669 http script var: "/forum/styles/bastisstyle/theme/stylesheet.css"
2012/08/08 16:24:20 [debug] 13885#0: *669 try to use file: "/forum/styles/bastisstyle/theme/stylesheet.css" "/srv/www/wanda.eu/forum/styles/bastisstyle/theme/stylesheet.css"
2012/08/08 16:24:20 [debug] 13885#0: *669 try file uri: "/forum/styles/bastisstyle/theme/stylesheet.css"
2012/08/08 16:24:20 [debug] 13885#0: *669 malloc: 000000000152A140:4096
2012/08/08 16:24:20 [debug] 13885#0: *669 http init upstream, client timer: 0
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "QUERY_STRING"
2012/08/08 16:24:20 [debug] 13885#0: *669 fastcgi param: "QUERY_STRING: "
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "REQUEST_METHOD"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script var: "GET"
2012/08/08 16:24:20 [debug] 13885#0: *669 fastcgi param: "REQUEST_METHOD: GET"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "CONTENT_TYPE"
2012/08/08 16:24:20 [debug] 13885#0: *669 fastcgi param: "CONTENT_TYPE: "
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "CONTENT_LENGTH"
2012/08/08 16:24:20 [debug] 13885#0: *669 fastcgi param: "CONTENT_LENGTH: "
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "SCRIPT_NAME"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script var: "/forum/styles/bastisstyle/theme/stylesheet.css"
2012/08/08 16:24:20 [debug] 13885#0: *669 fastcgi param: "SCRIPT_NAME: /forum/styles/bastisstyle/theme/stylesheet.css"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "REQUEST_URI"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script var: "/forum/styles/bastisstyle/theme/stylesheet.css"
2012/08/08 16:24:20 [debug] 13885#0: *669 fastcgi param: "REQUEST_URI: /forum/styles/bastisstyle/theme/stylesheet.css"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "DOCUMENT_URI"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script var: "/forum/styles/bastisstyle/theme/stylesheet.css"
2012/08/08 16:24:20 [debug] 13885#0: *669 fastcgi param: "DOCUMENT_URI: /forum/styles/bastisstyle/theme/stylesheet.css"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "DOCUMENT_ROOT"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script var: "/srv/www/wanda.eu"
2012/08/08 16:24:20 [debug] 13885#0: *669 fastcgi param: "DOCUMENT_ROOT: /srv/www/wanda.eu"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "SERVER_PROTOCOL"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script var: "HTTP/1.1"
2012/08/08 16:24:20 [debug] 13885#0: *669 fastcgi param: "SERVER_PROTOCOL: HTTP/1.1"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "GATEWAY_INTERFACECGI/1.1"
2012/08/08 16:24:20 [debug] 13885#0: *669 fastcgi param: "GATEWAY_INTERFACE: CGI/1.1"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "SERVER_SOFTWARE"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "nginx/"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script var: "0.7.67"
2012/08/08 16:24:20 [debug] 13885#0: *669 fastcgi param: "SERVER_SOFTWARE: nginx/0.7.67"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "REMOTE_ADDR"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script var: "95.130.252.148"
2012/08/08 16:24:20 [debug] 13885#0: *669 fastcgi param: "REMOTE_ADDR: 95.130.252.148"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "REMOTE_PORT"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script var: "62676"
2012/08/08 16:24:20 [debug] 13885#0: *669 fastcgi param: "REMOTE_PORT: 62676"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "SERVER_ADDR"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script var: "109.75.187.90"
2012/08/08 16:24:20 [debug] 13885#0: *669 fastcgi param: "SERVER_ADDR: 109.75.187.90"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "SERVER_PORT"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script var: "81"
2012/08/08 16:24:20 [debug] 13885#0: *669 fastcgi param: "SERVER_PORT: 81"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "SERVER_NAME"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script var: "www.wanda.eu"
2012/08/08 16:24:20 [debug] 13885#0: *669 fastcgi param: "SERVER_NAME: www.wanda.eu"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "REDIRECT_STATUS200"
2012/08/08 16:24:20 [debug] 13885#0: *669 fastcgi param: "REDIRECT_STATUS: 200"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "SCRIPT_FILENAME"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script copy: "/srv/www/wanda.eu"
2012/08/08 16:24:20 [debug] 13885#0: *669 http script var: "/forum/styles/bastisstyle/theme/stylesheet.css"
2012/08/08 16:24:20 [debug] 13885#0: *669 fastcgi param: "SCRIPT_FILENAME: /srv/www/wanda.eu/forum/styles/bastisstyle/theme/stylesheet.css"
2012/08/08 16:24:20 [debug] 13885#0: *669 http cleanup add: 000000000152A9E8
2012/08/08 16:24:20 [debug] 13885#0: *669 get rr peer, try: 1
2012/08/08 16:24:20 [debug] 13885#0: *669 socket 44
2012/08/08 16:24:20 [debug] 13885#0: *669 epoll add connection: fd:44 ev:80000005
2012/08/08 16:24:20 [debug] 13885#0: *669 connect to unix:/tmp/php-cgi/php-cgi.socket, fd:44 #684
2012/08/08 16:24:20 [debug] 13885#0: *669 connected
2012/08/08 16:24:20 [debug] 13885#0: *669 http upstream connect: 0
2012/08/08 16:24:20 [debug] 13885#0: *669 http upstream send request
2012/08/08 16:24:20 [debug] 13885#0: *669 chain writer buf fl:0 s:1816
2012/08/08 16:24:20 [debug] 13885#0: *669 chain writer in: 000000000152AA20
2012/08/08 16:24:20 [debug] 13885#0: *669 writev: 1816
2012/08/08 16:24:20 [debug] 13885#0: *669 chain writer out: 0000000000000000
2012/08/08 16:24:20 [debug] 13885#0: *669 event timer add: 44: 60000:1344435920767
2012/08/08 16:24:20 [debug] 13885#0: *669 http run request: "/forum/styles/bastisstyle/theme/stylesheet.css?"
2012/08/08 16:24:20 [debug] 13885#0: *669 http upstream check client, write event:1, "/forum/styles/bastisstyle/theme/stylesheet.css"
2012/08/08 16:24:20 [debug] 13885#0: *669 http upstream recv(): -1 (11: Resource temporarily unavailable)
2012/08/08 16:24:20 [debug] 13885#0: *669 http upstream request: "/forum/styles/bastisstyle/theme/stylesheet.css?"
2012/08/08 16:24:20 [debug] 13885#0: *669 http upstream process header
2012/08/08 16:24:20 [debug] 13885#0: *669 malloc: 000000000152FAB0:32768
2012/08/08 16:24:20 [debug] 13885#0: *669 recv: fd:44 12488 of 32768
2012/08/08 16:24:20 [debug] 13885#0: *669 http fastcgi record byte: 01
2012/08/08 16:24:20 [debug] 13885#0: *669 http fastcgi record byte: 06
2012/08/08 16:24:20 [debug] 13885#0: *669 http fastcgi record byte: 00
2012/08/08 16:24:20 [debug] 13885#0: *669 http fastcgi record byte: 01
2012/08/08 16:24:20 [debug] 13885#0: *669 http fastcgi record byte: 1F
2012/08/08 16:24:20 [debug] 13885#0: *669 http fastcgi record byte: F8
2012/08/08 16:24:20 [debug] 13885#0: *669 http fastcgi record byte: 00
2012/08/08 16:24:20 [debug] 13885#0: *669 http fastcgi record byte: 00
2012/08/08 16:24:20 [debug] 13885#0: *669 http fastcgi record length: 8184
2012/08/08 16:24:20 [debug] 13885#0: *669 http fastcgi parser: 0
2012/08/08 16:24:20 [debug] 13885#0: *669 http fastcgi header: "X-Powered-By: PHP/5.3.3-7+squeeze13"
2012/08/08 16:24:20 [debug] 13885#0: *669 http fastcgi parser: 0
2012/08/08 16:24:20 [debug] 13885#0: *669 http fastcgi header: "Content-type: text/html"

....还有好多行,表明它是由 nginx 发送出去的。

问题就在这里,内容类型被设置为text/html,并且很明显,php解释器为样式表运行,这是绝对没有必要的。

尤其是这几行让我感到困惑:

2012/08/08 16:24:20 [debug] 13885#0: *669 http script var: "/forum/styles/bastisstyle/theme/stylesheet.css"
2012/08/08 16:24:20 [debug] 13885#0: *669 try to use file: "/forum/styles/bastisstyle/theme/stylesheet.css" "/srv/www/wanda.eu/forum/styles/bastisstyle/theme/stylesheet.css"
2012/08/08 16:24:20 [debug] 13885#0: *669 try file uri: "/forum/styles/bastisstyle/theme/stylesheet.css"

nginx 正在正确的位置寻找正确的文件,但似乎无法识别它。这不是权限问题,因为当我以 www-data 用户(即 nginx 用户)的身份登录时,我可以打开该文件。

任何能帮助解决服务器为何会出现这种行为的问题的帮助都将不胜感激。

谨致问候,Jens

编辑1

我认为我自己解决了这个问题,尽管我不太清楚这个配置与第一个配置有何不同,以及为什么第一个配置示例中的行为与我描述的一样。我尝试修复我在该网站上遇到的另一个问题,它也修复了样式表的问题。

以下是有效的配置:

server {

  listen   81; ## listen for ipv4
  listen   [::]:81 default ipv6only=on; ## listen for ipv6

  server_name  www.wanda.eu;

  access_log  /var/log/nginx/wanda.eu.access.log;
  error_log   /var/log/nginx/wanda.eu.error.log debug;

  root /srv/www/wanda.eu;
  index index.php index.html;

  location /static {
    try_files $uri $uri/ @php_index =404;
    expires 24h;
  }

  location / {
    try_files $uri $uri/ $uri/index.html $uri/index.htm @php;
  }
  location /dynamic {
    try_files $uri $uri/ /dynamic/index.php =404;
    include /etc/nginx/fastcgi_params;
    fastcgi_index index.php;
    fastcgi_pass unix:/tmp/php-cgi/php-cgi.socket;
    fastcgi_param SCRIPT_FILENAME /srv/www/wanda.eu$fastcgi_script_name;
  }

  location @php {
    try_files $uri /index.php =404;
    include /etc/nginx/fastcgi_params;
    fastcgi_index index.php;
    fastcgi_pass unix:/tmp/php-cgi/php-cgi.socket;
   fastcgi_param SCRIPT_FILENAME /srv/www/wanda.eu$fastcgi_script_name;
  }

  location ~ \.php$ {
    try_files $uri /index.php =404;
    include /etc/nginx/fastcgi_params;
    fastcgi_index index.php;
    fastcgi_pass unix:/tmp/php-cgi/php-cgi.socket;
    fastcgi_param SCRIPT_FILENAME /srv/www/wanda.eu$fastcgi_script_name;
  }
}

答案1

您的问题的答案是,您告诉 nginx 每当请求与该位置匹配时,始终运行 PHP。无论它是什么类型的文件。让我详细说明一下。

location / {
    fastcgi_pass php;
}

现在,无论如何,与该位置匹配的任何内容都将由 PHP 处理。

location / {
    try_files $uri /index.php;
    fastcgi_pass php;
}

这不会改变任何东西!如果 $uri 匹配,nginx 将继续处理该fastcgi_pass php;指令。如果 $uri 不存在,它将执行内部重定向以/index.php继续处理该fastcgi_pass php;指令。

location / {
    try_files $uri @php;
}
location @php {
    fastcgi_pass php;
}

嗯,这有很大变化。如果 $uri 匹配,则无需处理,因此 nginx 将直接返回文件。如果 $uri 不匹配,nginx 将继续查看指定位置@php,然后转到该位置,然后遇到fastcgi_pass php;指令,当然它会处理该指令。

这应该可以回答您的具体问题。您自己已经找到了解决方案,但我认为您可能对为什么会这样感兴趣。

相关内容