Home >Operation and Maintenance >Nginx >How to solve the problem that docker nginx cannot be accessed after running

How to solve the problem that docker nginx cannot be accessed after running

WBOY
WBOYforward
2023-05-14 21:01:091372browse

## 1

I have recently been learning docker deployment, and initially planned to dockerize nginx first.

Contrast and customize the configuration

After copying the official nginx.conf, I modified and added some customizations, mainly blocking the default.conf and include folder sites-available

# include /etc/nginx/conf.d/.conf;
include /etc/nginx/sites-available/;

Official original configuration

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid    /var/run/nginx.pid;


events {
  worker_connections 1024;
}


http {
  include    /etc/nginx/mime.types;
  default_type application/octet-stream;

  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
           '$status $body_bytes_sent "$http_referer" '
           '"$http_user_agent" "$http_x_forwarded_for"';

  access_log /var/log/nginx/access.log main;

  sendfile    on;
  #tcp_nopush   on;

  keepalive_timeout 65;

  #gzip on;

  include /etc/nginx/conf.d/*.conf;
}

Create new docker-compose.yml. Simply specify images, name, port, and mount local files instead of the default

version: '3'
services:
 nginx-proxy:
  image: nginx
  container_name: nginx
  ports:
   - 8081:80
  volumes:
   - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro

## 2

After running docker-compose up, it has been stuck on attaching to nginx, and the browser cannot access the port address

starting nginx ... done
attaching to nginx

I don’t know where the problem lies. After searching for information, I found that I can use the tty parameter for debugging.

Modify docker-compose.yml and add a configuration tty:true.

docker exec -it nginx /bin/bash

I found that after deleting the default default.conf, I did not add other configuration files, and the previous sites-available folder was empty.

## 3

You have tricked yourself, add

-./nginx/sites-available:/etc/nginx/sites-available:ro

and add a configuration file in sites-available.

/etc/nginx/sites-available# ls
default.conf

After running, access to the port address is finally normal

docker nginx运行后无法访问如何解决

The above is the detailed content of How to solve the problem that docker nginx cannot be accessed after running. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete