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

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

我正在 OS X 上使用 NGINX 和 PHP-FPM 设置本地 Web 开发服务器。我已安装这两种服务并设置了虚拟域localhost。到目前为止一切正常:

  • nginx 运行正常
  • 它能够从为localhostserver_name 配置的自定义根目录读取文件
  • php 文件被正确处理

接下来我要设置的是域/phpmyadmin上路径的别名localhost。我希望 URL从中加载其内容,而不是从默认配置的根目录http://localhost/phpmyadmin加载。/usr/local/share/phpmyadmin

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

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

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

以下是我使用的配置:

/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/sites-available/default

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配置错误。我需要/usr/local/share像这样指向它:

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

但这仍然不起作用,我发现这是由于路径的配置方式造成的。这是一个相对符号链接。如果我创建一个指向绝对路径的新符号链接,它就会起作用。

例子:

/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作品。

关于如何配置 nginx 以允许从相对符号链接读取的任何想法?

答案1

添加index index.php到您的 PHPMyAdmin 块。这会告诉 nginx 请求目录时使用什么文件名。

相关内容