Independent implementation of nginx container configuration file

Independent implementation of nginx container configuration file

Create a container

[root@server1 ~]# docker run -it --name nginx1 -v /opt/data/web2:/web -p 81:80 centos:latest /bin/bash
[root@608de4875036 /]#

Enter the web directory and download the nginx package

[root@608de4875036 web]# wget http://nginx.org/download/nginx-1.20.1.tar.gz

Unzip the directory

[root@608de4875036 web]# ls
nginx-1.20.1 nginx-1.20.1.tar.gz

Install dependency packages

[root@608de4875036 web]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
[root@608de4875036 web]# yum -y groups mark install 'Development Tools'

Create User

[root@608de4875036 web]# useradd -r -M -s /sbin/nologin nginx
[root@608de4875036 web]# id nginx
uid=998(nginx) gid=996(nginx) groups=996(nginx)

Create a log storage file address

[root@6ad47178bdd6 web]# mkdir log

Compile and install

[root@608de4875036 web]# ls
log nginx-1.20.1 nginx-1.20.1.tar.gz
[root@608de4875036 web]# cd nginx-1.20.1
[root@608de4875036 nginx-1.20.1]# ls
auto conf html README
CHANGES configure LICENSE src
CHANGES.ru contrib man

[root@608de4875036 nginx-1.20.1]# ./configure \
--prefix=/web/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/web/log/access.log \
--error-log-path=/web/log/error.log

[root@608de4875036 nginx-1.20.1]# make && make install

View Catalog

[root@6ad47178bdd6 web]# ls
log nginx nginx-1.20.1 nginx-1.20.1.tar.gz


[root@608de4875036 web]# cd nginx
[root@608de4875036 nginx]# ls
conf html logs sbin

Configuring environment variables

[root@608de4875036 nginx]# ls
conf html logs sbin
[root@608de4875036 nginx]# cd sbin/
[root@608de4875036 sbin]# ls
nginx
[root@608de4875036 sbin]# pwd
/web/nginx/sbin

[root@608de4875036 sbin]# echo "export PATH=/web/nginx/sbin:\$PATH" > /etc/profile.d/nginx.sh
[root@608de4875036 sbin]# source /etc/profile.d/nginx.sh
[root@608de4875036 sbin]# which nginx
/web/nginx/sbin/nginx

Start the service

[root@608de4875036 sbin]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process         
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*   

View services on the container

[root@608de4875036 web]# ls
nginx nginx-1.20.1 nginx-1.20.1.tar.gz
[root@608de4875036 web]# cd nginx
[root@608de4875036 nginx]# ls
client_body_temp html sbin
conf logs scgi_temp
fastcgi_temp proxy_temp uwsgi_temp

View on the host machine

[root@server1 ~]# cd /opt/data/
[root@server1 data]# ls
web1 web2
[root@server1 data]# cd web2
[root@server1 web2]# ls
log nginx nginx-1.20.1 nginx-1.20.1.tar.gz

[root@server1 web2]# cd nginx
[root@server1 nginx]# ls
client_body_temp html sbin
conf logs scgi_temp
fastcgi_temp proxy_temp uwsgi_temp

You can see that the data has been synchronized

Modify the configuration file on the host

Create a directory named xy and copy the game code into this directory

[root@server1 html]# pwd
/opt/data/web2/nginx/html
[root@server1 html]# mkdir yx
[root@server1 html]# cd yx
[root@server1 yx]# ls
image index.html js

Create a directory test

[root@server1 html]# mkdir test
[root@server1 html]# ls
[root@server1 html]# ls
50x.html index.html test yx
[root@server1 html]# mv 50x.html index.html test/
[root@server1 html]# ls
test yx

Modify the nginx.conf configuration file

[root@server1 conf]# vi nginx.conf

........

    server {
         listen 8080;
         server_name test.example.com;

         location / {
             root /web/nginx/html/test; #File address in the container index index.html index.htm;
         }
    }

   server {
        listen 80;
        server_name xy.example.com;

        #charset koi8-r;

        #access_log logs/host.access.log main;

        location / {
            root /web/nginx/html/yx; #File address in the container index index.html index.htm;
        }
.....

But there is a problem with this modification. Only one port is mapped, and the other port is not mapped.

[root@server1 conf]# docker port 608de4875036
80/tcp -> 0.0.0.0:81
80/tcp -> :::81

How to resolve it?

Delete this container

[root@server1 ~]# docker stop 608de4875036
608de4875036
[root@server1 ~]# docker rm 608de4875036
608de4875036

The data here is still on the host machine

[root@server1 web2]# ls
log nginx nginx-1.20.1 nginx-1.20.1.tar.gz

Recreate this directory as a container mapping

[root@server1 ~]# docker run -it --name nginx2 -v /opt/data/web2:/web -p 80:80 -p 8080:8080 centos:latest /bin/bash
[root@6ad47178bdd6 /]#

View on the host machine

[root@server1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ad47178bdd6 centos:latest "/bin/bash" 23 seconds ago Up 22 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp nginx2
[root@server1 ~]# docker port 6ad47178bdd6
80/tcp -> 0.0.0.0:80
80/tcp -> :::80
8080/tcp -> 0.0.0.0:8080
8080/tcp -> :::8080

Check whether the data is synchronized in the container

[root@6ad47178bdd6 /]# ls
bin home lost+found opt run sys var
dev lib media proc sbin tmp web
etc lib64 mnt root srv usr
[root@6ad47178bdd6 /]# cd web/
[root@6ad47178bdd6 web]# ls
nginx nginx-1.20.1 nginx-1.20.1.tar.gz
[root@6ad47178bdd6 web]# cd nginx
[root@6ad47178bdd6 nginx]# ls
client_body_temp html sbin
conf logs scgi_temp
fastcgi_temp proxy_temp uwsgi_temp
#Data synchronization

Start the service

#Write an environment variable [root@6ad47178bdd6 /]# cat /etc/profile.d/nginx.sh 
export PATH=/web/nginx/sbin:$PATH
#Create nginx user [root@6ad47178bdd6 /]# useradd -r -M -s /sbin/nologin nginx
[root@6ad47178bdd6 /]# nginx
[root@6ad47178bdd6 /]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process         
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*                            
LISTEN 0 128 0.0.0.0:8080 0.0.0.0:*   

Visit 192.168.244.145:80

insert image description here

Visit 192.168.244.145:8080

insert image description here

This is the end of this article about the implementation of independent nginx container configuration files. For more related independent nginx container configuration files, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Nginx configuration file detailed explanation and optimization suggestions guide
  • How to view nginx configuration file path and resource file path
  • Detailed explanation of Nginx configuration file
  • Detailed explanation of nginx configuration file interpretation
  • Detailed explanation of Nginx static file service configuration and optimization

<<:  A brief discussion on the invalidation or implicit conversion of MySQL integer and string indexes

>>:  A brief discussion on HTML titles, paragraphs, line breaks, horizontal lines, and special characters

Recommend

What is WML?

WML (Wireless Markup Language). It is a markup la...

CSS to achieve glowing text and a little bit of JS special effects

Implementation ideas: Use text-shadow in CSS to a...

Solution to forgetting the root password of MySQL 5.7 and 8.0 database

Note: To crack the root password in MySQL5.7, you...

A brief discussion on the principle of shallow entry and deep exit of MySQL

Table of contents 1. Overview of the page 2. Infi...

HTML table tag tutorial (17): table title vertical alignment attribute VALIGN

The table caption can be placed above or below th...

JS generates unique ID methods: UUID and NanoID

Table of contents 1. Why NanoID is replacing UUID...

Basic notes on html and css (must read for front-end)

When I first came into contact with HTML, I alway...

Tutorial on binary compilation and installation of MySql centos7 under Linux

// It took me a whole afternoon to install this, ...

Detailed explanation of Nginx current limiting configuration

This article uses examples to explain the Nginx c...

my.cnf parameter configuration to optimize InnoDB engine performance

I have read countless my.cnf configurations on th...

How to control the startup order of docker compose services

summary Docker-compose can easily combine multipl...

The difference between div and table in speed, loading, web application, etc.

1: Differences in speed and loading methods The di...

JavaScript source code for Elimination

JavaScript to achieve the source code download ad...