2017-03-03 101 views
1

這裏是我的nginx.conf供應來example.com/srv/phabricator/phabricator/webroot/index.php的任何請求。我想改變功能,如果請求進入example.com/test則提供/home/phragile/public/index.php如何nginx的請求重定向到不同的路徑

daemon off; 
error_log stderr info; 
worker_processes 1; 
pid  /run/nginx.pid; 

events { 
    worker_connections 4096; 
    use epoll; 
} 

http { 
    include  /etc/nginx/mime.types; 
    default_type application/octet-stream; 
    sendfile  on; 
    keepalive_timeout 65; 
    gzip on; 
    client_max_body_size 200M; 
    client_body_buffer_size 200M; 

    map $http_upgrade $connection_upgrade { 
     default upgrade; 
     '' close; 
    } 

    upstream websocket_pool { 
     ip_hash; 
     server 127.0.0.1:22280; 
    } 

    server { 
     listen  *:80; 

     access_log /var/log/nginx/access.log; 
     error_log /var/log/nginx/error.log; 

     root /srv/phabricator/phabricator/webroot; 
     try_files $uri $uri/ /index.php; 

     location /.well-known/ { 
      root /srv/letsencrypt-webroot; 
     } 

     location/{ 
      index index.php; 

      if (!-f $request_filename) 
      { 
       rewrite ^/(.*)$ /index.php?__path__=/$1 last; 
       break; 
      } 
     } 

     location /index.php { 
      include /app/fastcgi.conf; 
      fastcgi_param PATH "/usr/local/bin:/usr/bin:/sbin:/usr/sbin:/bin"; 
      fastcgi_pass 127.0.0.1:9000; 
     } 

     location = /ws/ { 
      proxy_pass http://websocket_pool; 
      proxy_http_version 1.1; 
      proxy_set_header Upgrade $http_upgrade; 
      proxy_set_header Connection "upgrade"; 
      proxy_read_timeout 999999999; 
     } 
    } 
} 

我曾嘗試以下,但它不工作:

location /test { 
    root /home/phragile/public; 
} 

有人能告訴我需要在.conf文件添加什麼?

回答

1

您將需要使用alias而不是root,因爲您試圖將/test映射到/home/phragile/public,而後者不會以前者結束。有關更多信息,請參閱this document。您還需要在該位置執行PHP(請參閱您的location /index.php塊)。

您有一個非常具體的配置,旨在執行一個PHP文件。爲/test一般的解決方案可能是這樣的:

location ^~ /test { 
    alias /home/phragile/public; 
    if (!-e $request_filename) { rewrite^/test/index.php last; } 

    location ~ \.php$ { 
     if (!-f $request_filename) { return 404; } 

     include /app/fastcgi.conf; 
     fastcgi_param PATH "/usr/local/bin:/usr/bin:/sbin:/usr/sbin:/bin"; 
     fastcgi_pass 127.0.0.1:9000; 

     fastcgi_param SCRIPT_FILENAME $request_filename; 
    } 
} 

我已經粘貼現有的FastCGI指令(這我相信是爲你工作)和SCRIPT_FILENAME添加所需的值(假設你使用php_fpm或類似)。

當然,如果在/test下沒有靜態內容,則可以大大簡化。

+0

這個工作時,我嘗試了下面的URI:'http:// code.baqar.xgrid/test/index.php' 我需要改變什麼才能使它只使用'http://代碼。 baqar.xgrid/test /' –

+1

將「index index.php」語句移動到服務器塊中,以便它由新的位置塊繼承。 –

相關問題