Nginx subdirectory root with PHP

前端 未结 1 1348
小鲜肉
小鲜肉 2020-12-19 20:30

I\'m running nginx in a docker container. I want to have a subdirectory /web/ to access my personal files and projects. It should also support php.

Belo

相关标签:
1条回答
  • 2020-12-19 21:19

    If your files are in /etc/nginx/www you will need to use an alias directive, rather than a root directive. See this document for details.

    For example:

    location ^~ /web {
        index index.php index.html;
        alias /etc/nginx/www;
    
        if (!-e $request_filename) { rewrite ^ /web/index.php last; }
    
        location ~ \.php$ {
            if (!-f $request_filename) { return 404; }
    
            fastcgi_pass php:9000;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $request_filename;
        }
    }
    

    Use $request_filename to obtain the correct path to the aliased file. Avoid try_files with alias due to this issue. See this caution on the use of if.

    0 讨论(0)
提交回复
热议问题