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
Release: 2023-05-14 21:01:09
forward
1391 people have browsed it

## 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/;
Copy after login

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;
}
Copy after login

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
Copy after login

## 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
Copy after login

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
Copy after login

and add a configuration file in sites-available.

/etc/nginx/sites-available# ls
default.conf
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template