如何解决如何使用Nuxt + Laravel API设置NginX?
我正在尝试为使用Nuxt(SSR)来满足前端请求以及使用Laravel API进行后端内容和数据提取的应用设置简单的Web服务器。
我在同一台服务器上运行Laravel和Nuxt。到目前为止,这是我想到的:
map $sent_http_content_type $expires {
"text/html" epoch;
"text/html; charset=utf-8" epoch;
default off;
}
server {
listen 8005;
server_name localhost;
location / {
expires $expires;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 1m;
proxy_connect_timeout 1m;
proxy_pass http://127.0.0.1:3000;
}
location /photos {
root /home/user/laravel/storage/app/;
}
location ~ /\.ht {
deny all;
}
}
server {
listen 8010;
server_name localhost;
root /home/user/laravel/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
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/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
我已经这样配置axios的baseURL:
axios: {
baseURL: process.env.API_URL,},
.env:
API_URL="http://localhost:8010/api"
问题是Axios调用在SSR(首次渲染)期间工作,但不在客户端。显然是因为localhost主机名仅在服务器内部是已知的,而在客户端内部则不知道。
我希望我的意图很明确。那么,如何正确配置Nginx和Nuxt + Laravel使其成为可生产的堆栈呢?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。