不能使用多个项目

时间:2018-11-23 09:14:32

标签: laravel nginx location root

我正在尝试将nginx配置为按路径使用多个项目。

所以我有一个基于javascript(VueJs项目)的客户端,该客户端将请求发送到api。 api的根始于/api(Laravel项目)。 我也有基于Laravel的管理面板。管理面板的网址将以/admin开头。

这是我的nginx配置文件

server {
    server_name cabinet.mydomain.org; 

   # auth_basic "Restricted Content";
   # auth_basic_user_file /etc/nginx/.htpasswd;

   # access_log /var/www/cabinet/access.log;
   # error_log /var/www/cabinet/error.log;

    root /var/www/cabinet/api/html/public;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm;

    #location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
    #   try_files $uri $uri/ =404;
    #}

    location /api {
          access_log /var/log/cabinet.api.acc.log;
          error_log /var/www/cabinet.api.error.log debug;
          try_files $uri $uri /index.php$args;
        }

        location /admin {
          access_log /var/log/cabinet.admin.acc.log;
          error_log /var/www/cabinet.admin.error.log debug;
      root /var/www/cabinet-admin/public;
          try_files $uri $uri /index.php$args;
        }



        location / {
          try_files $uri $uri/ /index.html;
          root /var/www/cabinet/client/dist;
        }

    location = / {
          return 301 $scheme://$server_name/login/;
        }


#        location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
#       return 500; 
           # Some basic cache-control for static files to be sent to the browser
#           expires max;
#           add_header Pragma public;
#           add_header Cache-Control "public, must-revalidate, proxy-revalidate";
#       }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #

    location ~ \.php$ {
       include snippets/fastcgi-php.conf;
       fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}

 # managed by Certbot

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/cabinet.mydomain.org-0001/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/cabinet.mydomain.org-0001/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = cabinet.mydomain.org) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    #if ($host = cabinet.mydomain.com) {
    #    return 301 https://cabinet.mydomain.org$request_uri;
    #} # managed by Certbot

    #if ($host = cabinet.mydomain.org) {
    #    return 301 https://$host$request_uri;
    #} # managed by Certbot

    server_name cabinet.mydomain.org cabinet.mydomain.com;
    listen 80;
    return 301 https://cabinet.mydomain.org$request_uri;
    return 404; # managed by Certbot


}

因此,当尝试访问/admin时,服务器将重定向到/login

请帮助解决此问题

1 个答案:

答案 0 :(得分:1)

尝试一下。

server {
    server_name cabinet.mydomain.org; 

   # auth_basic "Restricted Content";
   # auth_basic_user_file /etc/nginx/.htpasswd;

   # access_log /var/www/cabinet/access.log;
   # error_log /var/www/cabinet/error.log;

    root /var/www;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm;

    #location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
    #   try_files $uri $uri/ =404;
    #}

    location /api {
          access_log /var/log/cabinet.api.acc.log;
          error_log /var/www/cabinet.api.error.log debug;
          root PATH_TO_YOUR_PROJECT;
          try_files $uri $uri /PATH_TO_PROJECT/index.php$args;
        }

        location /admin {
          access_log /var/log/cabinet.admin.acc.log;
          error_log /var/www/cabinet.admin.error.log debug;
          root /var/www/cabinet-admin/public;
          try_files $uri $uri /cabinet-admin/public/index.php$args;
        }

请遵循与

相关的文档

$try_files

root