我正在尝试提供一个 Vue 应用,它基本上提供静态文件。我有一个简单的配置,其中还包括后端 PHP API 服务器。
当我转到api.localhost
(我将其添加到hosts
文件中)时,我确实看到了后端 html 页面。但是当我转到时localhost
,它会将我发送到50x
错误页面:
user www-data;
http {
server {
listen 80;
server_name api.localhost;
root /var/www/php-backend/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
server {
listen 80;
server_name localhost;
location /var/www/vue-frontend/dist {
root /;
index index.html;
try_files $uri $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
events {}
当转到 时localhost
,我总是看到index.html
里面的页面,/usr/share/nginx/html;
而不是 Vue index.html
。为什么error.log
?/var/log/nginx
除了[notice]: signal process started
答案1
请修改 localhost 的服务器块中的位置块:
root /var/www/vue-frontend/dist;
location / {
index index.html;
try_files $uri $uri/ /index.html;
}
因为
location /var/www/vue-frontend/dist {
意味着
http://localhost/var/www/vue-frontend/dist/index.html
这不是你想要的。