在子文件夹中为 Laravel 配置 nginx
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27785372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Config nginx for Laravel In a subfolder
提问by Lu32
I have and old project that now requires new functionality, I'm going to use laravel to provide it, everything in working ok in xampp with apache but my server con nginx show me access denied message and cant access my routes, how should be my site config should be if laravel is installed in mysite.com/2015 my site config is the following, what showld I change? I have tried
我有一个现在需要新功能的旧项目,我将使用 laravel 来提供它,在 xampp 和 apache 中一切正常,但我的服务器 con nginx 向我显示访问被拒绝的消息并且无法访问我的路由,我的应该怎么办站点配置应该是如果在 mysite.com/2015 中安装了 laravel 我的站点配置如下,我改变了什么显示?我试过了
location /newsection/ {
try_files $uri $uri/ /newsection/public/index.php$request_uri;
}
but it causes 500 error
但它会导致 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 ^/(.+)/$ / 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/ 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;
}
}
回答by Lu32
Well, I found a solution to very easy config and install Laravel in a subdirectory in a nginx server, in the /etc/nginx/sites-available/yourSite config file, add this:
好吧,我找到了一个非常简单的配置解决方案,并将 Laravel 安装在 nginx 服务器的子目录中,在 /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?/ last;
}
and voila, your routes will work normally how they should.
瞧,您的路线将按应有的方式正常运行。
回答by Hamid Naghipour
after I spend some hours on this issue, finally I could fixed my site with a subdomain address :
我在这个问题上花了几个小时后,终于可以用子域地址修复我的网站:
If you want to put your laravel
project in a subfolder
on a server with ngnix-ubuntu 16-php.7.2
, so here is update ngnix config :
如果你想把你的laravel
项目subfolder
放在服务器上ngnix-ubuntu 16-php.7.2
,那么这里是更新 ngnix 配置:
1) your nested(subfolder) isn't inside your main folder
1)您的嵌套(子文件夹)不在您的主文件夹中
/var/www/main:
/var/www/nested:
then your config :
然后你的配置:
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?/ last;
}
2) The laravel-test folder (subfolder) inside the main folder :
2)主文件夹内的laravel-test文件夹(子文件夹):
/var/www/main:
/var/www/main/nested:
then your config :
然后你的配置:
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?/ last;
}
回答by rubens21
it did not work for me, so I got another solution.
它对我不起作用,所以我得到了另一个解决方案。
I had created a "normal" laravel domain, point to http://generic.laravelroot /my/laravel/path/public
After that, I created a location on real domain proxying to my generic Laravel:
location /laravel { rewrite /laravel/?(.*)$ / break; proxy_pass http://generic.laravel;
}
Unfortunately, Laravel is going to use the url http://generic.laravelto create links. You may to solve it following this steps Laravel: Change base URL?
我创建了一个“普通”的laravel域,指向http://generic.laravelroot /my/laravel/path/public
之后,我创建了一个真实域代理到我的通用 Laravel 的位置:
location /laravel { rewrite /laravel/?(.*)$ / break; proxy_pass http://generic.laravel;
}
不幸的是,Laravel 将使用 url http://generic.laravel来创建链接。您可以按照以下步骤解决Laravel: Change base URL?
回答by Pat M
For some reason for me, the alias was causing the issue and didn't work. So maybe this will help others, so here is what I did to make this work. As you can see I took the "alias" out of it and added laravel/public to the equation.
出于某种原因,别名导致了问题并且不起作用。所以也许这会帮助其他人,所以这就是我为使这项工作所做的工作。正如你所看到的,我去掉了“别名”,并将 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?/ last;
}
回答by kusum chouhan
location /stationery { alias /var/www/html/stationery/public; try_files $uri $uri/ @stationery; location ~ .php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.2-fpm.sock; fastcgi_param SCRIPT_FILENAME $request_filename; } }
location /stationery { 别名 /var/www/html/stationery/public; try_files $uri $uri/ @stationery; 位置 ~ .php$ { 包含片段/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.2-fpm.sock; fastcgi_param SCRIPT_FILENAME $request_filename; } }
location @stationery{ rewrite /stationery/(.*)$ /stationery/index.php?/ last; }
location @stationery{ rewrite /stationery/(.*)$ /stationery/index.php?/ last; }
回答by gfirm
please use this :
请使用这个:
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 ^/(.+)/$ / 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/ 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;
}
}
}