Score:0

nginx working fine in localhost but show 404 on server

ru flag

i have a laravel based project that i have to host on digital ocean using docker. docker is working fine on localhost but shows 404 on server but phpmyadmin working fine on server as well which is running on 8080 port.

Here's my docker-compose.yml

 version: '3'

networks:
  laravel:


services:
  nginx:
    image: nginx:stable-alpine
    container_name: nginx
    ports:
      - "8088:80"
    volumes:
      - ./src:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
      - mysql
    networks:
      - laravel

  mysql:
    image: mysql:5.7.22
    restart: unless-stopped
    tty: true
    ports:
      - "4306:3306"
    volumes:
      - ./mysql:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: "root"
      MYSQL_DATABASE: "laravel_test"
      MYSQL_USER: "root"
      MYSQL_PASSWORD: "root"
      SERVICE_TAGS: "dev"
      SERVICE_NAME: "mysql"
    networks:
      - laravel

  php:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: php
    volumes:
      - ./src:/var/www/html
    ports:
      - "9000:9000"
    restart: unless-stopped

    networks:
      - laravel

  phpmyadmin:
    image: phpmyadmin
    restart: always
    ports:
      - 8080:80
    environment:
      - PMA_ARBITRARY=1
    links:
      - mysql
    networks:
      - laravel

  npm:
    image: node:18-alpine3.14
    container_name: npm
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html
    entrypoint: [ 'npm', '--no-bin-links' ]
    networks:
      - laravel

  composer:
    image: composer:latest
    container_name: composer
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html
    depends_on:
      - php
    networks:
      - laravel

Dockerfile

FROM php:8.1.0-fpm

RUN docker-php-ext-install pdo pdo_mysql

default.conf

server{
    listen 80;
    index index.php index.html;
    server_name localhost;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html/public;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

my php file is in ./src/public/

ge flag
(1) Are you sure you're talking directly to the server and not to a caching proxy? (2) What path is within the URL that you're testing with? (3) Does that path match a filename relative to `/var/www/html/public`? (4) Does `/var/www/html/public/index.php` exist?
rootShiv avatar
ru flag
yes file does exist , and this is working on localhost. i didn't understood your 1,2 question . im new to docker and server work in general
ge flag
To confirm (1) simply check that _something_ appear in `/var/log/nginx/access.log` or `/var/log/nginx/error.log` when you try to access it from outside. If not then you're not actually connecting to _this_ server, something else is feeding you the 403 error. By (2) I mean, the URL with the `https://domain.name/` stripped off the front and any `?query=string` and `#anchor` stripped off the end. Further question: by "working on localhost", do you mean you're running a client (such as `curl` or `wget`) with a URL like `https://localhost/src/public/yourpage.php` ?
rootShiv avatar
ru flag
how do i access `/var/log/nginx/access.log` . im new to linux and ops side of development
ge flag
There are many _many_ options, so the simplest is to say "the same way that you access `./src/public/yourscript`"
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.