大家,早安,
我有一个问题,现在(严重)已经一个月了,我试图修复它。它在这里:我的堆栈是Symfony NginxVue.js。
api文件夹是PHP / Symfony4.4中的API代码。back文件夹是Vue.JS中的管理面板。
它们都是(git)子模块。
我关心的是Vuejs会议。要访问该应用程序,我们需要登录。在开发模式下,一切正常,但在生产模式下则不行!实际上,当我提交表格时;我有405不允许错误
我不知道发生了什么。这是我的项目conf。
api:
container_name: svisor-api
build:
context: ./api
env_file:
- ./api/.env
depends_on:
- db
restart: always
volumes:
- ./api:/var/www/api:rw
# - /var/www/html/vendor
nginx:
container_name: svisor-nginx
image: nginx:stable-alpine
depends_on:
- api
- website
- back
volumes:
- ./website:/var/www/html:rw
- ./api:/var/www/api:rw
- ./nginx.default.conf:/etc/nginx/conf.d/default.conf
- ./logs/nginx:/var/log/nginx
restart: always
ports:
- "80:80"
back:
container_name: svisor-back
build:
context: ./back
restart: always
根据官方文档,我这样做了
FROM node:lts-alpine as build-stage
RUN echo "================ BUILDING BACK =================="
RUN apk add npm
WORKDIR /app
COPY package*.json ./
COPY ./ .
RUN npm install
RUN npm run build
FROM nginx:stable-alpine as production-stage
RUN mkdir -p /app
COPY --from=build-stage /app /app COPY ./nginx/nginx.conf /etc/nginx/conf.d/default.conf
server {
listen 80 default_server;
server_name localhost;
root /app/dist;
index index.html;
location / {
try_files $uri $uri/ /index.html;
add_header 'Access-Control-Allow-Origin' '*';
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
//file vue.config.js
module.exports = {
devServer: {
proxy: process.env.VUE_APP_API_URL,
}
}
和登录代码:
actions: {
login({commit}, credentials) {
console.log(process.env.VUE_APP_URL);
console.log(process.env.VUE_APP_API_URL);
return axios
.post(process.env.VUE_APP_URL + '/login_check', credentials)
.then(({data}) => {
commit('SET_LOGGED_USER', data)
})
},
}
(console.log确认我是否在开发或生产环境中)。
### API server
server {
listen 80;
server_name api.svisor.localhost;
root /var/www/api/public;
access_log /var/log/nginx/api.access_log;
error_log /var/log/nginx/api.error_log;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /index.php$is_args$args;
add_header 'Access-Control-Allow-Origin' '*';
}
location ~ ^/index\.php(/|$) {
fastcgi_pass api:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
add_header 'Access-Control-Allow-Origin' '*';
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $document_root;
internal;
}
location ~ \.php$ {
return 404;
}
}
### Back server
server {
listen 80;
server_name back.svisor.localhost;
access_log /var/log/nginx/back.access_log;
error_log /var/log/nginx/back.error_log;
location / {
proxy_pass http://back;
}
error_page 400 402 403 404 /40x.html;
location = /40x.html {
root /usr/share/nginx/html;
}
}
I am pretty sure I have read 80% of google results (from page 1 to 10); I have search in github issues of every project. And it's always the same thing : CORS (Access-Control-Allow-Origin/Methods). I tried all that but nothing works. I even tried things I don't even remember (from 50x error to craziest ones). Some asking to implement an OPTIONS call but I don't know how. Looking at how many tickets there are on this topic in Laravel forum, it is obviously a big problem. Some CORS source I read : Mozilla Cross-Origin Resource Sharing (CORS) Cross-Origin Resource Sharing (CORS)
Sorry for all the text and I hope I gave enough data so that you can help me fix this.
Thank you in advance.
根据我所看到的,在开发人员模式下,所有未知请求都将VUE_APP_API_URL
根据doc被代理。因此,即使您在开发人员模式下的请求屏幕截图上显示为localhost:8080/login_check
,它也会被代理到您的VUE_APP_API_URL
目的地。
现在处于生产模式,您正在向发出发布请求VUE_APP_URL
。这只是用于静态文件的nginx服务器,没有代理。作为一个修复你可以用替换更新您的登录代码VUE_APP_URL
用VUE_APP_API_URL
。
return axios
.post(process.env.VUE_APP_API_URL + '/login_check', credentials)
.then(({data}) => {
commit('SET_LOGGED_USER', data)
})
开发模式代理仅适用于vue开发服务器。它不适用于生成的静态文件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句