1
3

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.

nginxでSSLアクセラレーター

1
Last updated at Posted at 2018-07-18

事前準備

yumリポジトリの設定

# cat << EOS > /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/`uname -m`/
gpgcheck=0
enabled=1
EOS

firewalld無効化

systemctl disable firewalld
systemctl stop firewalld

SELinux無効化

vi /etc/selinux/config

-SELINUX=enforcing
+SELINUX=disabled

インストールと起動

nginxをインストール

# yum install nginx

バージョンの確認

nginx -v

自動起動設定

systemctl enable nginx

起動

systemctl start nginx

デフォルトページの確認
表示の確認をする。 http://(FQDN)/
上記の index.html のデフォルトのパス
/usr/share/nginx/html/index.html
nginx の設定ファイルのパス
/etc/nginx/conf.d/default.conf

簡単なリバースプロキシ
/etc/nginx/conf.d 以下に XXX.confという命名でファイルを作成する
80 番で受けたリクエストを 8000 番に転送する

vi /etc/nginx/conf.d/server.conf

server {
listen 80;
# アクセス可能なIPアドレス、もしくはドメイン
server_name hogehoge.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_max_temp_file_size 0;
location / {
proxy_pass http://localhost:8000;
}
}

nginx基本コマンド
起動

systemctl start nginx

停止

systemctl stop nginx

再起動

systemctl restart nginx

再起動しても設定ファイルが反映されない場合など

nginx -s reload

状態の確認

systemctl status nginx

SSL設定

su -
cd ~
openssl genrsa 2048 > server.key
openssl req -new -key server.key > server.csr

Country Name (2 letter code) [XX]:(国名)
State or Province Name (full name) []:(都道府県)
Locality Name (eg, city) [Default City]:(市区町村)
Organization Name (eg, company) [Default Company Ltd]:(会社名)
Organizational Unit Name (eg, section) []:(部署名)
Common Name (eg, your name or your server's hostname) []:(FQDN)
Email Address []:(空)

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:(空)
An optional company name []:(空)

openssl x509 -days 3650 -req -signkey server.key < server.csr > server.crt

設定サンプル1

/etc/nginx/conf.d/default.conf

server {
    listen    80;
    server_name  [公開しているサーバー名];
                location / {
                                proxy_pass http://192.168.1.100; #プロキシ先サーバー
                }
                location /server-status {
                                stub_status on;
                                access_log off;
                                allow 127.0.0.1;
                                allow 192.168.1.1; #監視サーバー
                                deny all;
                }
}
 
server {
    listen    443 ssl;
    server_name  [公開しているサーバー名];
                location / {
                                proxy_set_header host              $host;
                                proxy_set_header X-Forwarded-For   $remote_addr;
                                proxy_set_header X-Forwarded-Proto https;
                                proxy_set_header X-Forwarded-Port  443;
                                # proxy_set_header Connection "keep-alive"; # header情報は適宜修正してください! 
                                proxy_pass http://192.168.1.100; #プロキシ先サーバー
                }
                location /server-status {
                                stub_status on;
                                access_log off;
                                allow 127.0.0.1;
                                allow 192.168.1.1; #監視サーバー
                                deny all;
                }
}

80番で受けたリクエストを8080番に転送する
server {
     listen 80;
     # アクセス可能なIPアドレス、もしくはドメイン
     server_name hogehoge.com;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header Host $http_host;
     proxy_redirect off;
     proxy_max_temp_file_size 0;
     location / {
        proxy_pass http://localhost:8000;
     }
}

設定サンプル2

vi /etc/nginx/conf.d/default.conf

server {
    listen    443 ssl;
    server_name 192.168.11.111;
#    ssl on;
    ssl_certificate     /etc/nginx/conf.d/server.crt;
    ssl_certificate_key /etc/nginx/conf.d/server.key;
    ssl_protocols         TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache    shared:SSL:10m;
    ssl_session_timeout  10m;


    location / {
        proxy_pass http://192.168.22.222;

        #proxy_next_upstream error timeout;
        proxy_set_header Host               $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-Port   $remote_port;
        port_in_redirect                    off;
        add_header      Front-End-Https     on;
        #real_ip_header X-Forwarded-For;
    }
}
1
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?