0

我正在尝试设置一个包含 3 种不同类型内容的 nginx 服务器:

  • 主网站,在 CodeIgniter 上运行
  • 子文件夹中的问答论坛/qa(在 Question2Answer 上运行)
  • 静态文件(在不同的位置,包括/qa

我遇到了各种麻烦。我当前的配置(在服务器块内)是:

# Q2A
if ($request_uri ~* "^/qa/") {
    rewrite ^/qa/(.*)$ /qa/index.php?qa-rewrite=$1 last;
}
# CI
if (!-e $request_filename) {
    rewrite ^(.+)$ /index.php?$1 last;
}
location / {
    index index.php index.html;
}
location ~ \.php$ {
        try_files $uri =404;

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

        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/site$fastcgi_script_name;
        fastcgi_param HTTPS off;
}

除了这些问题之外,这主要是有效的:

  • 正在解析/执行对我的应用程序文件夹中的 PHP 文件的请求。显然,由于这没有通过 CI 应用程序,因此会导致错误(未找到变量等)。
  • 文件夹内的所有静态文件qa都被传递给 Q2A 应用程序,而不是作为静态文件提供

我已经尝试了很多不同的东西,我已经记不清了,比如使用类似的位置块location ~* ^/qa/ {}和各种排列try_files但没有运气。我还尝试在 nginx 站点上修改这个 Wordpress 示例。它们中的大多数都以/qa/返回 404 结束。一些方法导致服务器提供原始 PHP 代码!

任何人都可以帮助设置正确的方法吗?

4

3 回答 3

1

代替

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

location ~ /qa/(.*)? {
    try_files $uri /qa/index.php?qa-rewrite=$1&$query_string;
}

也是块

if (!-e $request_filename) {
    rewrite ^(.+)$ /index.php?$1 last;
}

最好在/位置内移动并转换为try_files

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

如果您仍然遇到问题,请告诉我。

于 2013-07-13T23:56:45.490 回答
0

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

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

只需将 (insert) 替换为 log 和 root 指令中的服务器值。

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;
    }
}
于 2013-07-14T02:39:12.583 回答
0

如果是邪恶的。但是你可以使用try_files和一些位置块来完成同样的事情。

# 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
于 2013-07-14T00:11:44.303 回答