如何为Nginx位置设置不同的根?

我在OS X上使用NGINX和PHP-FPM设置了我的本地web dev服务器。我已经安装了两个服务,并为localhost域设置了一个虚拟的。 到目前为止一切正常。

  • nginx运行正常
  • 它能够从为localhost server_nameconfiguration的自定义根目录读取文件
  • PHP文件处理正确

接下来我要设置的是localhost域中的/phpmyadminpath的别名。 我想要url http://localhost/phpmyadmin/usr/local/share/phpmyadmin加载其内容,而不是从默认configuration的根目录。

我在本地主机服务器configuration中添加了这个位置块:

 location /phpmyadmin { alias /usr/local/share/phpmyadmin; include /usr/local/etc/nginx/conf.d/php-fpm; } 

http://localhost/phpmyadmin请求上的响应是404。

这里是我使用的configuration:

/usr/local/etc/nginx/nginx.conf

 worker_processes 2; error_log /usr/local/etc/nginx/logs/error.log debug; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /usr/local/etc/nginx/logs/access.log main; sendfile on; keepalive_timeout 600; gzip on; gzip_disable "msie6"; gzip_vary on; gzip_proxied any; gzip_comp_level 2; gzip_buffers 16 8k; gzip_http_version 1.1; gzip_min_length 256; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon; index index.html index.php; include /usr/local/etc/nginx/sites-enabled/*; } 

在/ usr / local / etc中/ nginx的/网站可用/默认

 server { listen 80; server_name localhost; root /Users/sebi/www/localhost; access_log /usr/local/etc/nginx/logs/default.access.log main; error_log /usr/local/etc/nginx/logs/default.error.log debug; location / { include /usr/local/etc/nginx/conf.d/php-fpm; } location /phpmyadmin { alias /usr/local/share/phpmyadmin; include /usr/local/etc/nginx/conf.d/php-fpm; } error_page 404 /404.html; error_page 403 /403.html; } 

/usr/local/etc/nginx/conf.d/php-fpm

 location ~ \.php$ { try_files $uri = 404; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } 

*更新*
我发现我的/phpmyadmin位置块是错误configuration的。 我需要像这样将它指向/usr/local/share

 location /phpmyadmin { alias /usr/local/share; include /usr/local/etc/nginx/conf.d/php-fpm; } 

但这仍然不起作用,我发现这是由于如何configurationpath。 这是一个相对的符号链接。 如果我创build一个新的符号链接到绝对path,它将工作。

例子:

/usr/local/share/phpmyadmin -> ../Cellar/phpmyadmin/4.7.4/share/phpmyadmin不起作用导致directory index of "/usr/local/share/" is forbidden错误

/usr/local/share/pma -> /usr/local/Cellar/phpmyadmin/4.7.4/share/phpmyadmin工作。

任何想法如何configurationnginx允许从相对符号链接读取?

index index.php添加到您的PHPMyAdmin块。 这告诉nginx请求一个目录的请求使用哪个文件名。