nginxでhttpsリバースプロキシを構築し.Net Coreアプリを動かす

■ 前提条件 $dotnet run で http://localhost:5000にアクセスするとdotnetアプリが動作すること。
前回のブログnginxでリバースプロキシさせてDotnet Coreを動かすで一通りリバースプロキシが構築できていること。
■ nginxの設定
ngingxのconfigファイルを以下の通り作成する。
httpsでアクセスが来たら、localhost:5000にリバースプロキシする。

./nginx.conf
$ cat nginx.conf 
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
	worker_connections 768;
	# multi_accept on;
}

http {
include        /etc/nginx/proxy_params;
	##
	# Basic Settings
	##

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
   server_names_hash_bucket_size 128;
	types_hash_max_size 2048;


    server_tokens off;
	include /etc/nginx/mime.types;
	default_type application/octet-stream;
  ##
  # Logging Settings
  ##

  access_log /etc/nginx/logs/access.log;
  error_log /etc/nginx/logs/error.log;

  upstream helloapp{
    server 127.0.0.1:5000;
  }

  server {
      listen     80;
      server_name  localhost;
      index        index.html;
      root /var/www/html;
  }

  server {
      listen                    10443 ssl;
      server_name               localhost 127.0.0.1;

      ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
	  ssl_prefer_server_ciphers on;

      ssl_certificate           /etc/nginx/ssl/server.crt;
      ssl_certificate_key       /etc/nginx/ssl/server.key;
      
      add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
      add_header X-Frame-Options DENY;
      add_header X-Content-Type-Options nosniff;

      #	Redirects all traffic
      location / {
        proxy_pass http://helloapp;
        proxy_http_version 1.1;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
      }
  }

	##
	# Gzip Settings
	##

	gzip on;

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}

./proxy_params
$ cat proxy_params
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-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $server_name;
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffers           32 4k;

./conf.d/app01.conf これは、暗号化なしの80→10080ポートフォワード
$ cat conf.d/app01.conf 
server {
    listen       10080;
    server_name  localhost 127.0.0.1;
    charset     utf-8;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location / {
      proxy_pass http://localhost:5000;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection keep-alive;
      proxy_set_header Host $host;
      proxy_cache_bypass $http_upgrade;
      proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header   X-Forwarded-Proto $scheme;
    }
}
これで
 http://192.168.33.10:10080
 https://192.168.33.10:10443
 でアクセスするとOK。
SSLはかならず、httpsでアクセスすること。
httpでアクセスすると、「400 The plain HTTP request was sent to HTTPS port」エラーが発生する。

コメント

このブログの人気の投稿

ソリューション構成ごとにconfigファイルを作成する

C++の古いプロジェクトのビルドでerror MIDL2311 : statements outside library block are illegal in mktyplib compatability mode

web.config debug/releaseの内容を変換構文を使って切り替える