1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pm2(express) + nginx + https + 複数サーバー同一ドメイン(proxy兼用)のためのnginx conf 設定

Last updated at Posted at 2018-04-10

設定でいつも困るのでメモとして。
色々ググると設定出てくるけど、結局どうなってればいいのかってのがわかりづらいので、今回設定した設定ファイルをそのまま残す。

この設定の目的

  • 複数サーバーで動かしている別々のwebアプリケーションを同一ドメイン内で動いているように見せる

やりたいこと

  • upstreamで動かしているローカルアプリケーションがメイン( location / )
  • とあるパス( location /<proxy_path> )だけはproxy機能を利用して外部サーバーへ接続させる

/etc/nginx/conf.d/app.conf

変数説明
<app_name>: アプリケーション名
<pm2_path>: pm2で起動したローカルアクセスパス(例: localhost:3000)
<DNS_server_IP>: DNS server IP address (AWSの場合server private ip addressxx.xx.xx.2 で固定らしい)
<domain>: なんでも良さそうだけど、複雑な設定するときはユニークな名前が必要なはず
<ssl_certificate_name>: ssl_certificate ファイル名
<ssl_certificate_key_name>: ssl_certificate_key ファイル名
<proxy_path>: proxy機能を利用して外部サーバーへ接続させるパス
<outer_server>: proxy機能を利用して接続する外部サーバーIP/ホスト名
upstream <app_name> {
  server <pm2_path>;
}

server {
  resolver <DNS_server_IP>; # ホスト名の名前解決ができない場合に設定する。無くても良い

  listen 80;
  listen 443 ssl;
  server_name <domain>;

  ssl on;
  ssl_certificate /etc/nginx/conf.d/<ssl_certificate_name>.crt; # ファイル配置場所はどこでもいい
  ssl_certificate_key /etc/nginx/conf.d/<ssl_certificate_key_name>.key;  # ファイル配置場所はどこでもいい
  ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers HIGH:!aNULL:!MD5;

  # 別のサーバーへアクセスさせる(proxy機能)
  location /<proxy_path> {
    set $domain <outer_server>; # 変数化することで名前解決済みのホストIPが変更されていても再度確認してくれるらしい
    rewrite ^/<proxy_path>/(.+) $1 break;
    proxy_pass http://$domain/<proxy_path>/$1$is_args$args;
  }

  location / {
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_redirect off;

    proxy_pass http://<app_name>;
  }

  # キャッシュ設定
  location ~ /.*\.(js|css|svg|jpg|gif|png|ico|woff) {
    expires 1d;
    access_log off;
    proxy_pass http://<app_name>;
  }

  index index.htm index.html;
  error_page 500 502 503 504 /500.html;
  client_max_body_size 100m;
  keepalive_timeout 5;
}

/etc/nginx/nginx.conf

  • ほとんど触ってないuserくらい
/etc/nginx/nginx.conf
user root;
worker_processes auto;
pid /run/nginx.pid;

events {
	worker_connections 768;
	# multi_accept on;
}

http {

	##
	# Basic Settings
	##

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	# server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	##
	# SSL Settings
	##

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

	##
	# Logging Settings
	##

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	##
	# Gzip Settings
	##

	gzip on;
	gzip_disable "msie6";

	# gzip_vary on;
	# gzip_proxied any;
	# gzip_comp_level 6;
	# gzip_buffers 16 8k;
	# gzip_http_version 1.1;
	# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

	##
	# Virtual Host Configs
	##

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

解決できなかったこと

  • 変なアクセス( $http_referer='http://www.baidu.com' みたいの )に対してブロックする return 403; break; 設定をしたかったけど、うまく制御できずに断念した
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?