Php 如何在nginx上提供论坛子文件夹中的静态文件?

Php 如何在nginx上提供论坛子文件夹中的静态文件?,php,configuration,nginx,Php,Configuration,Nginx,我正在尝试使用基本上3种不同类型的内容设置nginx服务器: 在CodeIgniter上运行的主网站 问答论坛,位于子文件夹/qa(运行问题2解答) 静态文件(位于不同位置,包括/qa) 我遇到了各种各样的麻烦。我当前的配置(在服务器块内)是: 除以下问题外,这主要是可行的: 正在解析/执行对我的应用程序文件夹中的PHP文件的请求。显然,由于这没有通过CI应用程序,因此会导致错误(未找到变量等) qa文件夹中的所有静态文件都将被传递到Q2A应用程序,而不是作为静态文件使用 我尝试了很多不

我正在尝试使用基本上3种不同类型的内容设置nginx服务器:

  • 在CodeIgniter上运行的主网站
  • 问答论坛,位于子文件夹
    /qa
    (运行问题2解答)
  • 静态文件(位于不同位置,包括
    /qa
我遇到了各种各样的麻烦。我当前的配置(在服务器块内)是:

除以下问题外,这主要是可行的:

  • 正在解析/执行对我的应用程序文件夹中的PHP文件的请求。显然,由于这没有通过CI应用程序,因此会导致错误(未找到变量等)
  • qa
    文件夹中的所有静态文件都将被传递到Q2A应用程序,而不是作为静态文件使用
我尝试了很多不同的东西,但都记不清了,比如使用像
location~*^/qa/{}
这样的位置块和
try_文件的各种排列方式,但运气不好。我也试过修改。他们中的大多数人只是以返回404而结束。有些方法导致服务器提供原始PHP代码

有人能帮我用正确的方法设置吗?

替换

if ($request_uri ~* "^/qa/") {
    rewrite ^/qa/(.*)$ /qa/index.php?qa-rewrite=$1 last;
}

还有街区

if (!-e $request_filename) {
    rewrite ^(.+)$ /index.php?$1 last;
}
最好在
/
位置内移动并转换为
try\u文件

location / {
    index index.php index.html;
    try_files $uri /index.php?$request_uri
}
如果您仍然有问题,请告诉我。

。但是您可以使用
try_文件
和一些位置块来完成相同的任务

# in a `server` block
index index.php index.html;

# case sensitive version
# location ~ ^/qa/(.*)?$ {
location  ~* ^/qa/(.*)?$ {
    try_files $uri /qa/index.php?qa-rewrite=$1;
}

location / {
    try_files $uri /index.php?$request_uri;
}

# not sure if you even need location /, this might work
# try_files $uri /index.php?$request_uri;

# the rest of your FastCGI config stuff here

这是基于我自己运行nginx的PHP站点的配置

注意这是未经测试的,但它应该可以工作,因为它只是一个稍微修改的版本

只需在log和root指令中用服务器值替换(insert)

server {
    listen 80;
    access_log  /var/log/nginx/(insert).access.log;
    error_log  /var/log/nginx/(insert).error.log;
    root (insert);
    server_name (insert);
    rewrite ^/qa/(.*(?![\.js|\.css])[^.]+)$ /qa/index.php/$1 last;
    rewrite ^(.*(?![\.js|\.css])[^.]+)$ /index.php/$1 last;
    location ~ [^/]\.php(/|$) {

        fastcgi_cache one;
        fastcgi_cache_key $scheme$host$request_uri;
        fastcgi_cache_valid  200 302 304 5m;
        fastcgi_cache_valid  301 1h;

        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

谢谢,但是出于某种原因,/qa目录中的页面不工作,除了目录本身,
mysite.com/qa/
。此外,我还尝试将其更改为
location~*^/qa/(.*)?$
,以确保它是整个URL,并提示下载PHP代码!嗯,我可能有点不对劲。这是Apache的原始规则:
RewriteRule^.*$index.php?qa rewrite=$0&%%{QUERY_STRING}[L]
就像我说的,如果我使用
location
它将下载实际的php代码,即下载文件
/qa/index.php
。为什么会这样?因为它没有将php代码传递给php引擎,所以它将php文件作为资产提供,您是否删除了
location~\.php$
location?不,我没有删除它。我的配置与我的问题中的配置一样,但有您建议的更改。这不起作用,在qa文件夹中,它提示下载PHP代码!
# in a `server` block
index index.php index.html;

# case sensitive version
# location ~ ^/qa/(.*)?$ {
location  ~* ^/qa/(.*)?$ {
    try_files $uri /qa/index.php?qa-rewrite=$1;
}

location / {
    try_files $uri /index.php?$request_uri;
}

# not sure if you even need location /, this might work
# try_files $uri /index.php?$request_uri;

# the rest of your FastCGI config stuff here
server {
    listen 80;
    access_log  /var/log/nginx/(insert).access.log;
    error_log  /var/log/nginx/(insert).error.log;
    root (insert);
    server_name (insert);
    rewrite ^/qa/(.*(?![\.js|\.css])[^.]+)$ /qa/index.php/$1 last;
    rewrite ^(.*(?![\.js|\.css])[^.]+)$ /index.php/$1 last;
    location ~ [^/]\.php(/|$) {

        fastcgi_cache one;
        fastcgi_cache_key $scheme$host$request_uri;
        fastcgi_cache_valid  200 302 304 5m;
        fastcgi_cache_valid  301 1h;

        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}