1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Nginx Configuration: POST Requests to Laravel API Returning Errors

Discussão em 'Outras Linguagens' iniciado por inanc eroglu, Novembro 3, 2024 às 20:02.

  1. inanc eroglu

    inanc eroglu Guest

    I am currently facing an issue with my Nginx configuration while trying to handle POST requests for my Laravel API. GET requests work without any problems, but POST requests are returning errors like 502 bad gateway.

    Here is my Nginx configuration for the API:

    server {
    listen 8080;
    server_name remote_addr;
    index index.php index.html;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/app/public;

    location /api {
    try_files $uri /index.php?$query_string;
    }

    location ~ \.php$ {
    include fastcgi_params;
    fastcgi_pass php:9000; # Changed to local PHP-FPM
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $path_info;
    }
    }


    here is docker-compose.yaml file


    version: '3.8'

    services:
    # Landing service (Next.js)
    landing:
    build:
    context: ./landing
    dockerfile: Dockerfile
    container_name: landing_page
    ports:
    - "3002:3000"
    networks:
    - app-network

    # Chat service (Next.js )
    chat:
    build:
    context: ./chat
    dockerfile: Dockerfile
    container_name: chat_app
    ports:
    - "3001:3000"
    networks:
    - app-network

    # Go service (for go-chat)
    go-chat:
    build:
    context: ./go-chat
    dockerfile: Dockerfile
    container_name: go_chat
    ports:
    - "7070:7070"
    networks:
    - app-network

    # Laravel backend service
    php:
    build:
    context: ./backend # Backend dizini
    dockerfile: Dockerfile
    container_name: laravel_backend
    restart: unless-stopped
    tty: true
    ports:
    - "9000:9000"
    volumes:
    - ./backend:/var/www/app:delegated # Uygulama dosyalarını bağlama
    networks:
    - app-network

    # PostgreSQL service
    postgres:
    image: postgres:15
    container_name: postgres_db
    environment:
    POSTGRES_DB: "project"
    POSTGRES_USER: "user"
    POSTGRES_PASSWORD: "pass"
    ports:
    - "5432:5432"
    volumes:
    - postgres_data:/var/lib/postgresql/data
    networks:
    - app-network

    # Nginx service
    nginx:
    image: nginx:alpine
    container_name: nginx_proxy
    restart: unless-stopped
    ports:
    - "8080:80"
    volumes:
    - ./backend:/var/www/app
    - ./nginx/conf.d/:/etc/nginx/conf.d/
    networks:
    - app-network
    depends_on:
    - chat
    - landing
    - php

    networks:
    app-network:
    driver: bridge

    volumes:
    postgres_data:



    and laravel Dockerfile


    # Base image
    FROM php:8.2-fpm

    # Install system dependencies
    RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl \
    libpq-dev \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

    # Install PHP extensions
    RUN docker-php-ext-install pdo pdo_pgsql

    # Set working directory
    WORKDIR /var/www/app

    # Copy application files
    COPY . ./

    # Install Composer
    RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

    # Install PHP dependencies
    RUN composer install --no-dev --optimize-autoloader

    # Set permissions
    RUN chown -R www-data:www-data /var/www/app/storage /var/www/app/bootstrap/cache \
    && chmod -R 775 /var/www/app/storage /var/www/app/bootstrap/cache

    # Expose port 9000 and start php-fpm server
    EXPOSE 9000
    CMD ["php-fpm"]



    thanks for helps

    I expected the POST requests to my Laravel API endpoints to work seamlessly after configuring Nginx and resolving the binding issues. Specifically, I anticipated that both GET and POST requests would function correctly, allowing for data retrieval and submission to my Laravel application without errors.

    Continue reading...

Compartilhe esta Página