Brew install nginx

Reference: blog.csdn.net/ncj39343790…

Brew install nginx brew info nginx nginx # reload nginx -s reload nginx -s reload # reopen nginx /opt/homebrew/etc/nginx/Copy the code

Nginx installation location:

/ opt/homebrew/Cellar/nginx / 1.21.1

Configuration file path:

/opt/homebrew/etc/nginx

Inner Nginx1 configuration port 20001, / TU forward to springboot project 8071.

server { listen 20001; Client_max_body_size 500M; server_name localhost; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; # gzip_http_version 1.0; gzip_comp_level 8; gzip_types gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_vary off; gzip_disable "MSIE [1-6]."; #charset koi8-r; # access_log /var/log/nginx/host.access.log main; # error_log /var/log/nginx/error.log crit: # add_header x-frame-options SAMEORIGIN; #iframe; Location /tu {# This machine started an 8071 SpringBoot service. proxy_pass http://localhost:8071/; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Nginx-Proxy true; proxy_redirect off; }}Copy the code

Docker install nginx

Reference: zhuanlan.zhihu.com/p/114603487

1. Pull Nginx

Docker pull nginx: Latest Pull complete View: Docker imagesCopy the code

\

2. Preparation

A working folder is created on the host for mounting configuration and static file access

(2.1) Docker run -d --name gwsnginx nginx -d tells Docker to run the split mode container in the background and print the new container ID. When using the --name flag, you can assign memorable names to Docker containers when they run, and the last one: nginx, which should be the name of the image!! (2.2). The conf file copy to the host machine # default configuration file to the local copy of container Nginx current directory under the conf directory ($PWD current the full path) docker cp gwsnginx: / etc/Nginx/Nginx. Conf /Users/konnysnow/dockerroot/nginx/conf docker cp gwsnginx:/etc/nginx/conf.d /Users/konnysnow/dockerroot/nginx/conf Delete docker container rm gwsnginx. Delete docker container rm gwsnginxCopy the code

3. Deploy the container

docker run -d -p 30001:80  \
              -p 30443:443  \
 --name gwsnginx \
 -v /Users/konnysnow/dockerroot/nginx/html:/usr/share/nginx/html \
 -v /Users/konnysnow/dockerroot/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
 -v /Users/konnysnow/dockerroot/nginx/conf/conf.d:/etc/nginx/conf.d \
 -v /Users/konnysnow/dockerroot/nginx/logs:/var/log/nginx \
 nginx 
Copy the code

-d # indicates that the container is always running in the background. -P 20001:80 # Maps the local port 20001 to port 80 inside the container

–name # set the name of the created container -v # mount the local directory (file) to the specified directory of the container;

— Link answer-server: answerServer this plan means you need to switch to the alias of the native Docker container.

The last nginx is the mirror name!!


Outer NginX2 configuration port 30001: forward to inner nginX-20001

The external host port is 30001

* * * * * * * * * * * * * * * * * * * *

server { listen 80; Listen [::]:80; server_name localhost; #access_log /var/log/nginx/host.access.log main; The location/tu {# docker, by the host. The docker. Internal: port, access to the host machine service proxy_pass http://host.docker.internal:20001/tu; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Nginx-Proxy true; proxy_redirect off; }}Copy the code

Three, test,

1.http://localhost:8071/user/getUserInfo/1

2,http://localhost:20001/tu/user/getUserInfo/1

3,http://localhost:30001/tu/user/getUserInfo/1

\

All three layers are ok.

Ok, a few key points:

  • 1. Docker accesses the host machine, host.docker.internal
  • 2.-p 20001:80

Host port: port in the container

  • 3.-v /Users/konnysnow/dockerroot/nginx/logs:/var/log/nginx

[Host directory file: directory file in container]