[英]How do I make my laravel app work on a Nginx server?
I just deployed my Laravel application on DigitalOcean using the LEMP stack with a Nginx server.我刚刚使用带有 Nginx 服务器的 LEMP 堆栈在 DigitalOcean 上部署了我的 Laravel 应用程序。
However when I navigate to my servers IP adress, I get a 403 Forbidden error.但是,当我导航到我的服务器 IP 地址时,出现 403 Forbidden 错误。 Then when I go to xxx.xx.xxx.xx/public, I can see my homepage correctly even though I set my root to my public folder already.然后,当我转到 xxx.xx.xxx.xx/public 时,即使我已经将根目录设置为公共文件夹,我也可以正确地看到我的主页。 Also, when I click on any link on my homepage, it gives me a 404 Not Found.此外,当我点击主页上的任何链接时,它会给我一个 404 Not Found。
This is my etc/nginx/sites-available/default:这是我的 etc/nginx/sites-available/default:
What am I doing wrong?我究竟做错了什么? Please don't mark my question as a duplicate, because most of similar topics I see they forget to add Index.php to their index list or forget to link their root to their public folder, however I'm doing both correctly.请不要将我的问题标记为重复,因为我看到的大多数类似主题都忘记将 Index.php 添加到他们的索引列表中或忘记将他们的根链接到他们的公共文件夹,但是我做的都是正确的。
My config file:我的配置文件:
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html/public/;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name 159.65.192.29;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
}
尝试在项目的根目录下将server.php
名称更改为index.php
,并使用以下内容创建一个nginx.conf
文件:
# nginx configuration location ~* (\.css|\.mp4|\.woff|\.js|\.png|\.jpg|\.gif|robots\.txt)$ { } location ~ /public/ { } location / { if (!-e $request_filename){ rewrite ^/(.*)/$ /$1 redirect; } if (!-e $request_filename){ rewrite ^(.*)$ /index.php break; } if (!-e $request_filename){ rewrite ^/(css|js|images|favicon|fonts|videos|storage)/(.*)$ /public/$1/$2 break; } }
I use this on my server.我在我的服务器上使用它。 test it:测试一下:
server {
root /var/www/html/public;
server_name [your_ip_address];
index index.php;
# Load configuration files for the default server block.
location / {
try_files $uri $uri/ /index.php?$query_string;
}
## Laravel Production
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 = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location ~ /\.(?!well-known).* {
deny all;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.