search
HomeOperation and MaintenanceNginxHow to install Nginx and modify Nginx configuration file with Docker

1. Docker installation of Nginx

1. First, make sure you have started docker on the virtual machine.

How to install Nginx and modify Nginx configuration file with Docker

2. Secondly, log in to the DockerHub official website, then search for nginx, and then enter docker pull nginx in the virtual machine to download the nginx image. .

How to install Nginx and modify Nginx configuration file with Docker

3. After downloading, please use the docker images command to check whether the download is successful!

How to install Nginx and modify Nginx configuration file with Docker

4. Start nginx, enter the following statement, and press Enter. Here I will briefly talk about each parameter.

(1)–name: Determine the name of the container.

(2)-d specifies that the container is running in the background.

(3)-p Container exposed port number.

(4) nginx specifies the image. Because what we download here is the latest version of nginx, we don’t need to add the version number. However, if the download is not the latest version, you need to add the version number. For example, nginx: 1.2.45, that’s about it!

docker run \
--name ng \
-d \
-p 80:80 \
nginx

5. At this time, you can use docker -ps -a to check whether your container is running!

How to install Nginx and modify Nginx configuration file with Docker

2. Modify the docker configuration file:

1. Modify the docker configuration file here. The core is to use -v Parameters to bind the data volume to.

2. But how do you know what the nginx configuration file looks like? How do you know where the nginx configuration file is? Here you need to visit the official website of dockerHub to find the answer. ! We found through checking the official website that the path of nginx's html file is in /usr/share/nginx/html, and the nginx configuration file is mainly in /etc/nginx/nginx.conf!

How to install Nginx and modify Nginx configuration file with Docker

How to install Nginx and modify Nginx configuration file with Docker

3. Therefore, here we can use the cp command in docker to copy it out, and then Binding data volumes is in progress! !

(1) First create the corresponding folder:

mkdir -p \
/tmp/nginx/html \
/tmp/nginx/conf

(2) Secondly, copy the configuration file and folder to the specified directory of the host. Note that you must ensure that your The nginx container is already running! !

docker cp ng:/etc/nginx/nginx.conf /tmp/nginx/conf/

(3) Then you can check /tmp/nginx/conf, as shown in the figure below:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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;
     
}  
}

(4) Delete the previous nginx container and enter docker rm -f ng,That’s it!

(5) Re-create an nginx container and enter the following code block:

docker run \
--name ng \
-d \
-v /tmp/nginx/html:/usr/share/nginx/html \
-v /tmp/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-p 80:80 \
nginx

(6) After starting, we can modify the configuration file!

3. Redefine the nginx configuration file:

1. What should I do if I want to redefine a server?

How to install Nginx and modify Nginx configuration file with Docker

Reason: Be sure to comment out the include /etc/nginx/conf.d/.conf line first because /etc/nginx/ There is a default.conf default configuration in conf.d/.conf. If you do not comment it, the default configuration will still be used first!

2. How to solve the cross-domain problem? There are two methods:

(1) The first method: continue writing in the redefined server.

  server {
	listen 80;
	server_name testVite;
	location / {
	root   /usr/share/nginx/index12;
	index  index.html index.htm;
	}
	 location /api/ {
		proxy_pass http://192.168.37.1:8086/;
	}
}

(2) The second method is not to redefine the server, but to write it in the default.conf file of nginx. Then this method requires going through the above process of this blog again. First, The default file is copied using the cp command, and then the nginx container is re-created, mounted using the -v parameter, and then configured across domains!

The above is the detailed content of How to install Nginx and modify Nginx configuration file with Docker. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Deploying Applications with NGINX Unit: A GuideDeploying Applications with NGINX Unit: A GuideMay 04, 2025 am 12:03 AM

NGINXUnitischosenfordeployingapplicationsduetoitsflexibility,easeofuse,andabilitytohandledynamicapplications.1)ItsupportsmultipleprogramminglanguageslikePython,PHP,Node.js,andJava.2)Itallowsdynamicreconfigurationwithoutdowntime.3)ItusesJSONforconfigu

NGINX and Web Hosting: Serving Files and Managing TrafficNGINX and Web Hosting: Serving Files and Managing TrafficMay 03, 2025 am 12:14 AM

NGINX can be used to serve files and manage traffic. 1) Configure NGINX service static files: define the listening port and file directory. 2) Implement load balancing and traffic management: Use upstream module and cache policies to optimize performance.

NGINX vs. Apache: Comparing Web Server TechnologiesNGINX vs. Apache: Comparing Web Server TechnologiesMay 02, 2025 am 12:08 AM

NGINX is suitable for handling high concurrency and static content, while Apache is suitable for dynamic content and complex URL rewrites. 1.NGINX adopts an event-driven model, suitable for high concurrency. 2. Apache uses process or thread model, which is suitable for dynamic content. 3. NGINX configuration is simple, Apache configuration is complex but more flexible.

NGINX and Apache: Deployment and ConfigurationNGINX and Apache: Deployment and ConfigurationMay 01, 2025 am 12:08 AM

NGINX and Apache each have their own advantages, and the choice depends on the specific needs. 1.NGINX is suitable for high concurrency, with simple deployment, and configuration examples include virtual hosts and reverse proxy. 2. Apache is suitable for complex configurations and is equally simple to deploy. Configuration examples include virtual hosts and URL rewrites.

NGINX Unit's Purpose: Running Web ApplicationsNGINX Unit's Purpose: Running Web ApplicationsApr 30, 2025 am 12:06 AM

The purpose of NGINXUnit is to simplify the deployment and management of web applications. Its advantages include: 1) Supports multiple programming languages, such as Python, PHP, Go, Java and Node.js; 2) Provides dynamic configuration and automatic reloading functions; 3) manages application lifecycle through a unified API; 4) Adopt an asynchronous I/O model to support high concurrency and load balancing.

NGINX: An Introduction to the High-Performance Web ServerNGINX: An Introduction to the High-Performance Web ServerApr 29, 2025 am 12:02 AM

NGINX started in 2002 and was developed by IgorSysoev to solve the C10k problem. 1.NGINX is a high-performance web server, an event-driven asynchronous architecture, suitable for high concurrency. 2. Provide advanced functions such as reverse proxy, load balancing and caching to improve system performance and reliability. 3. Optimization techniques include adjusting the number of worker processes, enabling Gzip compression, using HTTP/2 and security configuration.

NGINX vs. Apache: A Look at Their ArchitecturesNGINX vs. Apache: A Look at Their ArchitecturesApr 28, 2025 am 12:13 AM

The main architecture difference between NGINX and Apache is that NGINX adopts event-driven, asynchronous non-blocking model, while Apache uses process or thread model. 1) NGINX efficiently handles high-concurrent connections through event loops and I/O multiplexing mechanisms, suitable for static content and reverse proxy. 2) Apache adopts a multi-process or multi-threaded model, which is highly stable but has high resource consumption, and is suitable for scenarios where rich module expansion is required.

NGINX vs. Apache: Examining the Pros and ConsNGINX vs. Apache: Examining the Pros and ConsApr 27, 2025 am 12:05 AM

NGINX is suitable for handling high concurrent and static content, while Apache is suitable for complex configurations and dynamic content. 1. NGINX efficiently handles concurrent connections, suitable for high-traffic scenarios, but requires additional configuration when processing dynamic content. 2. Apache provides rich modules and flexible configurations, which are suitable for complex needs, but have poor high concurrency performance.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft