在子文件夹中为Laravel配置nginx

o4tp2gmn  于 2023-01-31  发布在  Nginx
关注(0)|答案(9)|浏览(189)

我有一个旧项目,现在需要新的功能,我打算使用laravel来提供它,一切都在工作正常的xampp与Apache,但我的服务器con nginx显示我访问被拒绝的消息,无法访问我的路由,应该如何是我的网站配置应该是如果laravel安装在mysite.com/2015我的网站配置如下,什么showld我改变?我已经尝试

location /newsection/ { 
   try_files $uri $uri/ /newsection/public/index.php$request_uri;
}

但它会导致500错误

server {
    listen 80;
    server_name am2.aminversiones.com;
    root /home/forge/am2.aminversiones.com;

    # FORGE SSL (DO NOT REMOVE!)
    # ssl_certificate;
    # ssl_certificate_key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    index index.html index.htm index.php;

    charset utf-8;

    client_max_body_size 300M;

    location / {
        #try_files $uri $uri/ /index.php?$query_string;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/am2.aminversiones.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       fastcgi_pass unix:/var/run/php5-fpm.sock;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
    }

    # Remove trailing slash to please routing system.
    if (!-d $request_filename) {
        rewrite     ^/(.+)/$ /$1 permanent;
    }

    # version 1
    location ^~ /2015 {
        alias /home/forge/am2.aminversiones.com/2015/public;
        try_files $uri $uri/ @2015;
        location ~* \.php {
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            include /etc/nginx/fastcgi_params;
        }
    }

    location @2015 {
        rewrite ^/2015/(.*)$ /2015/index.php/$1 last; # THIS IS THE IMPORTANT LINE
    }
    # end version 1

    # version 2
    # this is with `ln -s /home/tom/public_html/demos/demo1/public <document root>/demo1`
    location ~ /2015 {
        try_files /2015/$uri /2015/$uri/ /2015/index.php?q=$uri&$args;
    }
    # end version 2

    # PHP FPM configuration.
    location ~* \.php$ {
        fastcgi_pass                    unix:/var/run/php5-fpm.sock;
        include                             /etc/nginx/fastcgi_params;
        fastcgi_index                       index.php;
        fastcgi_split_path_info             ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO             $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED       $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME       $document_root$fastcgi_script_name;
    }

    # We don't need .ht files with nginx.
    location ~ /\.ht {
        deny all;
    }
}
3duebb1j

3duebb1j1#

好吧,我找到了一个解决方案,可以非常容易地在nginx服务器的子目录中配置和安装Laravel,在/etc/nginx/sites-available/yourSite配置文件中,添加以下内容:

location ^~ /laravel {
    alias /var/www/laravel/public;
    try_files $uri $uri/ @laravel;

    location ~ \.php {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        include /etc/nginx/fastcgi_params;
    }
}

location @laravel {
    rewrite /laravel/(.*)$ /laravel/index.php?/$1 last;
}

瞧,你的路线将正常工作,他们应该如何。

aiazj4mn

aiazj4mn2#

在我花了几个小时在这个问题上之后,最后我用一个子域地址解决了我的问题,如下所示:
如果你想把你的laravel项目放到一个ngnix-ubuntu 16-php.7.2服务器上的subfolder中,那么下面是ngnix配置:
1.你的嵌套(子文件夹)不在你的主文件夹内
/var/网址/主目录:/var/www/嵌套:
则您的配置应为:

location /nested {

        alias /var/www/nested/public;

        try_files $uri $uri/ @nested;

               location ~ \.php$ {
                        include fastcgi_params;
                        fastcgi_param SCRIPT_FILENAME $request_filename;
                        fastcgi_pass   unix:/run/php/php7.2-fpm.sock;

                                }
   }

location @nested {
        rewrite /nested/(.*)$ /nested/index.php?/$1 last;
}

1.主文件夹中的laravel-test文件夹(子文件夹):
/var/网址/主目录:/var/www/主目录/嵌套目录:
则您的配置应为:

location /laravel-test {

    alias /var/www/main/laravel-test/public;

    try_files $uri $uri/ @laravelTest;

           location ~ \.php$ {
                    include fastcgi_params;
                    fastcgi_param SCRIPT_FILENAME $request_filename;
                    fastcgi_pass   unix:/run/php/php7.2-fpm.sock;

                            }

  }

location @laravelTest {
        rewrite /laravel-test/(.*)$ /laravel-test/index.php?/$1 last;
}
uhry853o

uhry853o3#

这是解决我的别名问题的变通方法,Nginx不像使用root指令那样查找/var/www/portal/public/portal/foo中的文件

location /portal {
 alias /var/www/html/portal/public; #preferred over root

 # @portal is a named location
 try_files $uri $uri/ @portal;

 location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $request_filename;
 }
}

location @portal {
rewrite /portal/(.*)$ /portal/index.php last; # Remove ?/$1 since fastcgi_params adds query string
}

其他参考可从本文https://gist.github.com/tsolar/8d45ed05bcff8eb75404中找到

ht4b089n

ht4b089n4#

出于某种原因,别名导致了这个问题,并且不起作用。因此,也许这会帮助其他人,所以下面是我为使这个问题起作用所做的工作。正如你所看到的,我去掉了“别名”,并将laravel/public添加到等式中。

location ^~ /laravel/public {
            index home.php home.html home.htm index.html index.htm index.php;
            try_files $uri $uri/ @laravel;

            add_header X-Frame-Options "SAMEORIGIN";
            add_header X-XSS-Protection "1; mode=block";
            add_header X-Content-Type-Options "nosniff";
            charset utf-8;

            location ~ \.php {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                fastcgi_buffers 16 16k;
                fastcgi_buffer_size 32k;
                include fastcgi_params;
            }
        }
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }

    location @laravel {
       rewrite /laravel/public/(.*)$ /laravel/public/index.php?/$1 last;
    }
xv8emn3q

xv8emn3q5#

它对我不起作用,所以我得到了另一个解决方案。
1.我创建了一个“普通”的laravel域,指向http://generic.laravel根目录/my/laravel/path/public
1.之后,我在真实的域上创建了一个位置来代理我的通用Laravel:

location /laravel {
    rewrite /laravel/?(.*)$ /$1 break;
    proxy_pass http://generic.laravel;

}
1.不幸的是,Laravel将使用url http://generic.laravel来创建链接。您可以按照以下步骤Laravel: Change base URL?来解决它

oprakyz7

oprakyz76#

我遇到过几个类似错误403或404的情况,我认为这个方法是有效的.不要忘记根据你在服务器上的php版本(php8.1-fpm.sock)来修改php版本.注意your_domain www.your_domain是基于你的网站.

server {
    listen 80;
    listen [::]:80;
    root /var/www/html;
    index index.html index.htm index.php;
    server_name your_domain www.your_domain;
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location /yourlaravelproject {
        alias /var/www/html/yourlaravelproject/public;
        try_files $uri $uri/ /index.php$args;

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        }
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    }
    location ~ /\.ht {
        deny all;
    }
}

配置后,不要忘记添加这个到你的符号链接,然后systemctl restart nginx。我敢肯定,为Laravel 8和9仍然工作,因为我最近刚刚测试的子文件夹中的Laravel 9项目和工程罚款。

wwtsj6pe

wwtsj6pe7#

在server指令的root参数中指定公共文件夹
在文件/etc/nginx/sites-available/am2.aminversiones.com

server {
    ...
    root /home/forge/am2.aminversiones.com/public;
    ...
}

重新启动nginx服务:sudo systemctl restart nginx

jtjikinw

jtjikinw8#

地点/文具{别名/var/www/html/文具/公共场所;尝试文件$uri $uri/@信纸;位置~. php ${包含代码段/fastcgi-php. conf;文件名:/run/php/php7.2-fpm. sock;快速cgi参数脚本文件名$请求文件名;}}
位置@信纸{重写/信纸/(. *)$/信纸/index.php?/最后;}

tkqqtvp1

tkqqtvp19#

请用这个:

server {
client_body_in_file_only clean;
client_body_buffer_size 32K;

client_max_body_size 300M;

sendfile on;
send_timeout 300s;
# Port that the web server will listen on.
#listen          80;

# Host that will serve this project.
server_name     tsolar.com;

# Useful logs for debug.
access_log      /var/log/nginx/tsolar.com-access.log;
error_log       /var/log/nginx/tsolar.com-error.log;
rewrite_log     on;

# The location of our projects public directory.
root            /home/tom/public_html/demos/;

# Point index to the Laravel front controller.
index           index.php;

location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}

location / {

    # URLs to attempt, including pretty ones.
    try_files $uri $uri/ /index.php?q=$uri&$args;
}

# Remove trailing slash to please routing system.
if (!-d $request_filename) {
    rewrite     ^/(.+)/$ /$1 permanent;
}

# version 1
location ^~ /demo1 {
    alias /home/tom/public_html/demos/demo1/public;
    try_files $uri $uri/ @demo1;

    location ~* \.php {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        include /etc/nginx/fastcgi_params;
    }
}

location @demo1 {
    rewrite ^/demo1/(.*)$ /demo1/index.php/$1 last; # THIS IS THE IMPORTANT LINE
}
# end version 1

# version 2
# this is with `ln -s /home/tom/public_html/demos/demo1/public <document root>/demo1`
location ~ /demo1 {
    try_files /demo1/$uri /demo1/$uri/ /demo1/index.php?q=$uri&$args;
}
# end version 2

# PHP FPM configuration.
location ~* \.php$ {
    fastcgi_pass                    unix:/var/run/php5-fpm.sock;
    include                             /etc/nginx/fastcgi_params;
    fastcgi_index                       index.php;
    fastcgi_split_path_info             ^(.+\.php)(/.+)$;
    fastcgi_param PATH_INFO             $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED       $document_root$fastcgi_path_info;
    fastcgi_param SCRIPT_FILENAME       $document_root$fastcgi_script_name;
}

# We don't need .ht files with nginx.
location ~ /\.ht {
    deny all;
}

# Set header expirations on per-project basis
location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff)$ {
    expires 365d;
}

}

相关问题