docker 多应用 nginx 配置

2022-03-04   


nginx 基础

nginx 的两种安装方式

方式一:直接安装

sudo apt install nginx

该方式会安装到 /etc/nginx 目录下,目录如下图:

image-20230301144016714

其中 nginx.conf 是主要的 nginx 配置,其中会引用其它位置的 nginx 配置,一般用户会将自己的配置放到 sites-available 目录中,再用一个软链接到 sites-enabled 目录生效自己的配置。

方式二:编译安装

apt -y install gcc make libssl-dev zlib1g-dev libgd-dev libgeoip-dev libpcre2-dev libpcre3-dev # 安装必要依赖
wget http://nginx.org/download/nginx-1.22.2.tar.gz # 下载nginx源码包
tar -xvf nginx-1.22.2.tar.gz
cd nginx-1.22.2# 解压后进入到解压目录
./configure 
make && make install

nginx 被安装到 /usr/local/nginx

image-20230302175725784

配置文件在 conf 目录下:

image-20230302180321588

原本没有红框的文件夹,为了配置管理的方便,可以自己新建 sites-availablesites-enabled 文件夹,在外面的 nginx.conf 把自己的配置目录包含进去即可:

# nginx.conf
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;

    include /usr/local/nginx/conf/sites-enabled/*;
}

nginx 重要配置介绍

场景描述

外界通过 80 和 443 访问,经过 Linux 主机的 nginx 反向代理到不同的 docker 应用容器中进行处理

Linux 主机 nginx 配置

应用 1 的 nginx 配置:

该应用通过域名 librariestoner.comwww.librariestoner.com 访问:

# sites-available/halo.conf
upstream halo {
  server 127.0.0.1:6080;
}
server {
  listen 80;
  listen [::]:80;
  server_name librariestoner.com www.librariestoner.com;
  client_max_body_size 1024m;
  return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name librariestoner.com www.librariestoner.com;

    client_max_body_size 1024m;

    ssl_certificate "cert/librariestoner/librariestoner.com_bundle.crt";

    ssl_certificate_key "cert/librariestoner/librariestoner.com.key";
    location / {
            proxy_pass http://halo;
            proxy_set_header HOST $host;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

应用 2 的 nginx 配置:

该应用通过域名 app817.acapp.acwing.com.cn 访问:

# sites-available/acapp.conf
upstream acapp {
    server 127.0.0.1:7080;
}
server {
  listen 80;
  listen [::]:80;
  server_name app817.acapp.acwing.com.cn;
  client_max_body_size 1024m;
  return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name app817.acapp.acwing.com.cn;
    client_max_body_size 1024m;
    ssl_certificate "cert/app.pem";

    ssl_certificate_key "cert/app.key";
    location / {
            proxy_pass http://acapp;
            proxy_set_header HOST $host;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location /wss {
        proxy_pass http://127.0.0.1:5015;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

注: https 要在最外层 nginx 配置中做。docker nginx 配置中只处理 80 端口。

docker1 nginx 配置

# sites-available/halo.conf
upstream halo {
  server 127.0.0.1:8090;
}
server {
  listen 80;
  listen [::]:80;
  server_name librariestoner.com www.librariestoner.com localhost;
  client_max_body_size 1024m;
  location / {
    proxy_pass http://halo;
    proxy_set_header HOST $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

docker2 nginx 配置

# sites-available/acapp.conf
server {
	listen 80;

    server_name localhost app817.acapp.acwing.com.cn;
	charset utf-8;
	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	client_max_body_size 10M;

	location / {
		include /etc/nginx/uwsgi_params;
		uwsgi_pass 127.0.0.1:8000;
		uwsgi_read_timeout 60;

		if ($request_method = 'OPTIONS') {
			add_header 'Access-Control-Allow-Origin' '*';
            # add_header 'Access-Control-Allow-Origin' 'https://www.acwing.com';
			add_header 'Access-Control-Allow-Methods' 'GET, PUT, OPTIONS, POST, DELETE';
			add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Amz-Date';
			add_header 'Access-Control-Max-Age' 86400;
			add_header 'Content-Type' 'text/html; charset=utf-8';
			add_header 'Content-Length' 0;
			return 204;
		}
		if ($request_method = 'PUT') {
			add_header 'Access-Control-Allow-Origin' '*';
            # add_header 'Access-Control-Allow-Origin' 'https://www.acwing.com';
			add_header 'Access-Control-Allow-Methods' 'GET, PUT, OPTIONS, POST, DELETE';
			add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Amz-Date';
			add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
		}
		if ($request_method = 'GET') {
			add_header 'Access-Control-Allow-Origin' '*';
			add_header 'Access-Control-Allow-Methods' 'GET, PUT, OPTIONS, POST, DELETE';
			add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Amz-Date';
			add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
		}
		if ($request_method = 'POST') {
			add_header 'Access-Control-Allow-Origin' '*';
			add_header 'Access-Control-Allow-Methods' 'GET, PUT, OPTIONS, POST, DELETE';
			add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Amz-Date';
			add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
		}
		if ($request_method = 'DELETE') {
			add_header 'Access-Control-Allow-Origin' '*';
			add_header 'Access-Control-Allow-Methods' 'GET, PUT, OPTIONS, POST, DELETE';
			add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Amz-Date';
			add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
		}
	}
	location /static {
		alias /home/acs/acapp/static/;
		if ($request_method = 'OPTIONS') {
			add_header 'Access-Control-Allow-Origin' '*';
			add_header 'Access-Control-Allow-Methods' 'GET, PUT, OPTIONS, POST, DELETE';
			add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Amz-Date';
			add_header 'Access-Control-Max-Age' 86400;
			add_header 'Content-Type' 'text/html; charset=utf-8';
			add_header 'Content-Length' 0;
			return 204;
		}
		if ($request_method = 'PUT') {
			add_header 'Access-Control-Allow-Origin' '*';
			add_header 'Access-Control-Allow-Methods' 'GET, PUT, OPTIONS, POST, DELETE';
			add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Amz-Date';
			add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
		}
		if ($request_method = 'GET') {
			add_header 'Access-Control-Allow-Origin' '*';
			add_header 'Access-Control-Allow-Methods' 'GET, PUT, OPTIONS, POST, DELETE';
			add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Amz-Date';
			add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
		}
	}
	location /media {
		alias /home/acs/acapp/media/;
		if ($request_method = 'OPTIONS') {
			add_header 'Access-Control-Allow-Origin' '*';
			add_header 'Access-Control-Allow-Methods' 'GET, PUT, OPTIONS, POST, DELETE';
			add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Amz-Date';
			add_header 'Access-Control-Max-Age' 86400;
			add_header 'Content-Type' 'text/html; charset=utf-8';
			add_header 'Content-Length' 0;
			return 204;
		}
		if ($request_method = 'PUT') {
			add_header 'Access-Control-Allow-Origin' '*';
			add_header 'Access-Control-Allow-Methods' 'GET, PUT, OPTIONS, POST, DELETE';
			add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Amz-Date';
			add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
		}
		if ($request_method = 'GET') {
			add_header 'Access-Control-Allow-Origin' '*';
			add_header 'Access-Control-Allow-Methods' 'GET, PUT, OPTIONS, POST, DELETE';
			add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Amz-Date';
			add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
		}
	}
}

原神派蒙动态表情包

Q.E.D.


我是星,利剑开刃寒光锋芒的银星,绝不消隐