简体   繁体   中英

Nginx except root, all others are redirected to 404 error

My goal is to put two or more(in future) Laravel applications under single domain and do the aliasing eg: http://domain-name/Lara-1 and http://domain-name/Lara-2 . I had trouble in aliasing and finally was able to do it by using locations but now, Except the root Laravel index.php of both apps, all other uris result in 404 error . Followed is my sites-available/default file:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

   index index.php index.html index.htm;
   try_files $uri $uri/ /index.php?$query_string;

    server_name Domain-IP;

     location ^~ /Lara-1/ {
        alias /var/www/Lara-1/public/;

        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php5.6-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        include fastcgi_params;
         }

     location ^~ /Lara-2/ {
     alias /var/www/Lara-2/public/;
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php5.6-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    include fastcgi_params;
    }

  location ~ /\.ht {
            deny all;
    }
 }

In-case of single app instead of two ie only Lara-1, when I put the root as /var/www/Lara-1/public/; everything works fine. Above situation happens only in case of app alias. May be, i may have configured it wrong.

Followed is my fastcgi_params file:

 fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REQUEST_SCHEME $scheme; fastcgi_param HTTPS $https if_not_empty; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; # PHP only, required if PHP was built with --enable-force-cgi-redirect fastcgi_param REDIRECT_STATUS 200; 

I am a newbie to Nginx and Laravel. Appreciate your help!

Got this apps working after trials and errors. Following is how my Nginx default file looks like with 2 Laravel apps placed under webroot dir. Happened to use Nginx Url Rewrites. Hope it helps!

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    index index.php index.html index.htm;

    server_name 172.3.21.1;
    root /var/www/webroot/;

location /projectdev {
        rewrite ^/projectdev/(.*)$ /projectdev/public/index.php?$1 last;
    }       

location /projectqa {
        rewrite ^/projectqa/(.*)$ /projectqa/public/index.php?$1 last;
    }
    error_page 401 403 404 /404.html;

location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

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

    location ~* .(woff|eot|ttf|svg|mp4|webm|jpg|jpeg|png|gif|ico|css|js)$ {
               expires 365d;
    }
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
            deny all;
    }
    # Block web attacks
    location ~* (roundcube|webdav|smtp|http\:|soap|w00tw00t)
    {
    return 444;
    }

    # Protect .ini files
    location ~ /\.ini
    {
    access_log off;
    log_not_found off;
    deny all;
    }
     location ~ /\.xml {
            deny all;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM