axios-module icon indicating copy to clipboard operation
axios-module copied to clipboard

Axios Proxy not working on server

Open dany68 opened this issue 4 years ago • 2 comments

Hi everyone,

I'm struggling for few days to find a solution. It seems that the proxy configuration is not taken into account when the site is on production. Locally everything is working fine but once the site is on the server my ajax calls hit mysite.com/api/ect... instead of being proxy to mysite.com/api/v1/ect. I tried to play with axios.baseURL and various configuration but nothing seems to work.

axios: {
    proxy: true,
    credentials: true,
},

proxy: {
    '/api/': { target: 'mysite.com/api/v1', pathRewrite: {'^/api/': ''} },
},

Maybe the issue comes from my Nginx configuration ? I use a reverse proxy to serve a nuxt app on mysite.com and a laravel api on mysite.com/api. Can this be the problem ?

#519 FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/mysite.com/before/*;

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name mysite.com;
    server_tokens off;
    root /home/forge/api/public;

    # FORGE SSL (DO NOT REMOVE!)
    ssl_certificate /etc/nginx/ssl/....
    ssl_certificate_key /etc/nginx/ssl/....

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers .....
    ssl_prefer_server_ciphers off;
    ssl_dhparam /etc/nginx/dhparams.pem;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    # FORGE CONFIG (DO NOT REMOVE!)
    include forge-conf/mysite.com/server/*;

    location /api {
        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; }

    access_log off;
    error_log  /var/log/nginx/mysite.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
    
    location / {
        proxy_pass http://127.0.0.1:3000;
        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;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/mysite.com/after/*;

Thanks for your help

dany68 avatar Aug 15 '21 20:08 dany68

To update: the issue came from the pathRewrite option that doesn't seems to take effect. I end up doing an axios plugin that directly do the path rewrite.

dany68 avatar Aug 20 '21 17:08 dany68

axios: {
    proxy: true,
    credentials: true,
    prefix: '/api/',
},

try it!

initmn avatar Oct 25 '21 14:10 initmn