douji9518 2016-09-14 17:57
浏览 33

nginx,每个子目录有一个单独的根

I'm trying to setup nginx so each path has it's own root directory. This is working for the most part, however POST to php-fpm a throwing a 405.

Currently trying:

    location ^~ /foo {
        alias /www/foo;
        #index  index.php;
        try_files $uri /www/foo/index.php$request_uri;
        access_log /var/log/nginx/foo.log main;
        error_log /var/log/nginx/foo.log error;
   }

    location ^~ /bar {
        alias /www/bar;
        #index  index.php;
        try_files $uri /www2/bar/index.php$request_uri;
        access_log /var/log/nginx/bar.log main;
        error_log /var/log/nginx/bar.log error;
   }

    location ~ \.php {
        set $php_root /usr/local/deploy/baz/current/web;
        if ($request_uri ~* /foo ) {
            set $php_root /www/foo/current/web;
        }
        if ($request_uri ~* /bar ) {
            set $php_root /www2/bar/current/web;
        }

        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $php_root$fastcgi_script_name;
        fastcgi_param  DOCUMENT_ROOT    $php_root;
        include        fastcgi_params;
} 
  • 写回答

1条回答 默认 最新

  • dtwxmn8741 2016-09-14 20:54
    关注

    Alias does not append location path to the file path. Check the logical flow. If server root is at /var/www/public, foo is located at /var/www/foo/public, bar is located at /var/www/bar/public. Then, this will be the easy config :

    server {
    root /var/www/public;
    ...
    location /foo {
         root /var/www/foo/public;
         }
    location ~ /foo/.+\.php$ {
        fastcgi_param  SCRIPT_FILENAME /var/www/foo/public$fastcgi_script_name;
        # rest of fastcgi
    }
    
    location /bar {
        root /var/www/bar/public;
    }
    
    location ~ /bar/.+\.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param      SCRIPT_FILENAME/var/www/bar/public$fastcgi_script_name;
        # rest of fastcgi
    }
    } # ends server
    

    If you want to use PHP in one directive then :

    server {
    ...
    root /var/www/public;
    ...
    location /foo {
         root /var/www/foo/public;
         }
    location /bar {
         root /var/www/bar/public;
         }
    
    location ~ \.php$ {
        set $php_root /var/www/public;
        if ($request_uri ~* /foo) {
            set $php_root /var/www/foo/public;
        }
        if ($request_uri ~* /bar) {
            set $php_root /var/www/bar/public;
        }
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $php_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
            }
    ...
     } # server block ends
    

    Usage of alias -- http://nginx.org/en/docs/http/ngx_http_core_module.html#alias Nginx modifier -- http://nginx.org/en/docs/http/ngx_http_core_module.html#location

    If you need more number of path, then you have to symlink.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100