我有它使用不同的端口上运行前端和后端水疗项目。
当我使用PHP工匠服务它可以成功运行。
但现在我想在不nginx的PHP工匠用来运行laravel后端。
# backend
server {
listen 3000 default_server;
listen [::]:3000 default_server;
root /usr/nextJs/nextTestBackend/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
}
nginx的开始后,127.0.0.1:3000/api/GET/users不能访问laravel后端。
它会显示错误:
Failed to load http://127.0.0.1:3000/api/GET/test: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1' is therefore not allowed access. The response had HTTP status code 500.
如何解决呢? 即使我把这个里面的位置{}它仍然得到同样的错误。
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
顺便说一句,在laravel的API也有CORS中间件。
Route::middleware('cors')->get('/GET/users', 'UserController@read');