LoginSignup
2
2

More than 5 years have passed since last update.

[個人的な備忘録とコピペ用]nginxでリバースプロキシ

Posted at

目的

  • nginxの基本的なことを知っておく。
  • nginxのインストールする。
  • 簡単なHTTPサーバーを2台立てる。
  • nginxでリバースプロキシする。
  • できる限りコピペで動くようにする。

作業ログ

今回は、RaspberryPi3上で作業する。

外へ見せるポートは8080
基本となるイメージがresin/rpi-raspbian
イメージ名がnginx_reverse_proxy

sudo docker run --name nginx_reverse_proxy -p 8080:80 -it -d resin/rpi-raspbian

パッケージをインストールする。
vimを入れているのは編集用。
python3は簡易サーバー用

sudo apt-get update && sudo apt-get install -y nginx vim python3

プロキシ先の開発用HTTPサーバーを用意する

サーバー1台目を用意

$ mkdir server1
$ cd server1/
$ cat server1 > server.html
$ python3 -m http.server 8000  >/dev/null 2>&1 &

動作確認

$ curl localhost:8000/server.html
server1

サーバー2台目を用意

$ mkdir server2
$ cd server2/
$ cat server2 > server.html
$ python3 -m http.server 9000  >/dev/null 2>&1 &

動作確認

$ curl localhost:9000/server.html
server2

ジョブの状態

$ jobs
[3]-  Running                 python3 -m http.server 8000 > /dev/null 2>&1 &  (wd: ~/server1)
[4]+  Running                 python3 -m http.server 9000 > /dev/null 2>&1 &  (wd: ~/server2)

ファイル構造

$ tree ~
/root
├── server1
│   └── server.html
└── server2
    └── server.html

2 directories, 2 files

nginxの設定ファイルの書き換え

自分が参考にしたデフォルト設定を表示(今回はsites-enabled/defaultの方で設定)

cat /etc/nginx/sites-enabled/default 
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
    #
    #   # With php5-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;
    #   # With php5-fpm:
    #   fastcgi_pass unix:/var/run/php5-fpm.sock;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}

一応バックアップをとる

$ cp /etc/nginx/sites-enabled/default /tmp/default.org

/server1/のパスで来た時には、ポート8000をlistenしてるサーバーへプロキシ(1台目)
/server2/のパスで来た時には、ポート9000をlistenしてるサーバーへプロキシ(2台目)

diff /etc/nginx/sites-enabled/default /tmp/default.org 
37,44d36
<   location /server1 {
<       proxy_pass http://localhost:8000/;
<   }
< 
<   location /server2 {
<       proxy_pass http://localhost:9000/;
<   }
< 

設定反映

とりあえずnginx自体を再起動して設定反映

$ sudo service nginx restart

動作確認

#1台目から返っているのを確認
$ curl 192.168.0.7:8080/server1/server.html
server1

#2台目から返っているのを確認
$ curl 192.168.0.7:8080/server2/server.html
server2
2
2
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
2
2