繁体   English   中英

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

[英]How to serve static files in forum subfolder on nginx?

我正在尝试设置基本上具有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代码!

谁能提供正确的方法来进行设置?

更换

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
}

如果您仍然遇到麻烦,请告诉我。

如果是邪恶的 但是,您可以使用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

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

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

只需用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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM