在由 Docker 提供支持的 NGINX 中启用 Laravel CORS
Posted
技术标签:
【中文标题】在由 Docker 提供支持的 NGINX 中启用 Laravel CORS【英文标题】:Enable Laravel CORS in NGINX powered by Docker 【发布时间】:2020-08-31 02:35:22 【问题描述】:我的 nginx 和 laravel 有我无法解决的问题。
我目前的设置是后端的 laravel,前端的 nuxt,一切都由 Docker compose 运行。我无法设置 cors 工作。
Docker-compose.yml
version: '3'
services:
nginx:
image: nginx:stable-alpine
container_name: a-nginx
ports:
- 88:80
volumes:
- ./server:/var/www/html
- ./default.conf:/etc/nginx/conf.d/default.conf
links:
- php
- nuxt
php:
build:
context: .
dockerfile: Dockerfile
container_name: a-php
volumes:
- ./server:/var/www/html
- ./custom.ini:/usr/local/etc/php/conf.d/custom.ini
ports:
- "9000:9000"
nuxt:
image: node:10.15.1
container_name: a-client
command: npm run dev
volumes:
- ./client:/usr/src/app
working_dir: /usr/src/app
ports:
- "3000:3000"
environment:
HOST: 0.0.0.0
default.conf
server
listen 80;
charset utf-8;
location /
proxy_redirect off;
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_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_pass http://a-client:3000;
location ~ ^/(api|storage)/
proxy_pass http://a-nginx:81;
服务器 听 81;
index index.php index.html;
root /var/www/api/public;
charset utf-8;
location /
try_files $uri $uri/ /index.php?$is_args$args;
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_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_read_timeout 1000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
location ~ /\.(?!well-known).*
deny all;
我基本上尝试了互联网上的所有解决方案,但都没有。
客户端上的控制台显示此警告: Client console
【问题讨论】:
【参考方案1】:在你的 nginx 配置中尝试adding CORS headers:
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Max-Age 3600;
add_header Access-Control-Expose-Headers Content-Length;
add_header Access-Control-Allow-Headers Range;
在 nuxt 中启用 CORS: 见CORS blocking client request in Nuxt.js
【讨论】:
以上是关于在由 Docker 提供支持的 NGINX 中启用 Laravel CORS的主要内容,如果未能解决你的问题,请参考以下文章
Nginx 与 Docker : nginx: [emerg] 未知指令“启用”在 /etc/nginx/nginx.conf