首页
学习
活动
专区
圈层
工具
发布

Nginx配置
EN

Stack Overflow用户
提问于 2012-03-05 02:35:09
回答 1查看 1.6K关注 0票数 1

我正在查看几个文档,但由于某些原因,我无法理解我所要做的配置。基本上我想:

所有目录请求都应保留其url,但应由

  • 处理各自目录中的.js、.css、.jpeg和.png文件(/js/、/css/和/
  • /),而不使用php
  • 则应拒绝其他文件访问(包括所有.php)。

我尝试过设置.php处理的位置,但我永远也找不出如何使它正常工作。我将继续做下去,但也希望能得到一些快速的帮助:)

谢谢!

编辑2:我想我想出了一些像样的东西。它可能不是完美的,但它应该有效。

代码语言:javascript
复制
server {
    listen 80;

    server_name _;
    server_tokens off;

    root /mypath/www;

    access_log /mypath/access.log;
    error_log /mypath/error.log;

    index index.html;

    location ^~ /app {
        return 404;
    }

    location ~ \.conf {
        return 404;
    }

    location ~ \.php {
        return 404 ;
    }

    location ~ /images/.*.(jpg|jpeg|png|gif) {
        try_files $uri $uri = 404;
        expires max;
    }

    location ~ /css/.*.css {
        try_files $uri $uri = 404;
        expires 1d;
    }

    location ~ /js/.*.js {
        try_files $uri $uri = 404;
        expires 1d;
    }

    location / {
        try_files $uri @PHPProxy;
        error_page 500 501 502 503 504 505 $document_root/error.html;
    }

    location @PHPProxy {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php.socket;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-05 03:33:56

想起来挺有趣的。我以前只是确保阻止目录或确保我没有任何文件,我不希望被访问在我的文档根目录。

我想我找到办法了。它实际上并没有为其他文件提供404或403,它只是将除指定目录之外的所有内容重写到/index.php。

下面是我用它测试的配置:

代码语言:javascript
复制
server {
listen 80;
listen your.domain.name:80;
server_name your.domain.name;

root /var/www/vhost/testing;
server_tokens off;

location ~ ^/(images|css|js)/.* {
    expires max;
}
location / {
    index index.php;
    rewrite ^(.*)$ /index.php$1 last;
}
location ~ ^/index.php {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param PATH_INFO $fastcgi_script_name;
    include /etc/nginx/fastcgi_params;
}
}

因此,/images、/css或/js中的任何内容都只是加载,但其他所有内容都会重写到索引文件中。

请记住,没有在这3个文件夹中的任何文件都将被完全忽略。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9560979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档