2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Apacheを立ててnginxでリバースプロキシ(特定URLに認証付き)

image.png

この記事でやらないこと

証明書などを使用

環境

ubuntu 22.04
nginx/1.18.0 (Ubuntu)
Apache/2.4.52 (Ubuntu)

1. Apacheをポート:8080番で立ち上げる

sudo apt update -y
sudo apt install apache2 -y
sudo nano /var/www/html/index.html
/var/www/html/index.html
<html><body><h1>Hello</h1></body></html>

8080に修正して立ち上げ

sudo nano /etc/apache2/ports.conf
sudo nano /etc/apache2/sites-available/000-default.conf
sudo systemctl start apache2
sudo systemctl enable apache2
# /etc/apache2/ports.conf
- Listen 80
+ Listen 8080

# /etc/apache2/sites-available/000-default.conf
- <VirtualHost *:80>
+ <VirtualHost *:8080>

2. nginxを80番で立ち上げ、リバースプロキシ設定をする

nginxをインストール、Basic認証用の設定

sudo apt update
sudo apt install nginx apache2-utils -y
sudo htpasswd -c /etc/nginx/.htpasswd <YOUR_NAME> # -cオプションは新規作成/上書き
sudo nano  /etc/nginx/nginx.conf
/etc/nginx/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 {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        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 TLSv1.3; # Dropping SSLv3, ref: POO>
        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_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/javascrip>

        ##
        # Virtual Host Configs
        ##

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

        server {
                listen  80;
                listen [::]:80;
                server_name     _;
                root    /usr/share/nginx/html;
                index index.html index.htm;

        location / {
                proxy_pass      http://localhost:8080;
                }
                
        location /admin/ {
                proxy_pass      http://localhost:8080;  
                auth_basic "auth";
                auth_basic_user_file    /etc/nginx/.htpasswd;
                }

        error_page 404 /404.html;
                location = /40x.html {
                }

        error_page 500 502 503 504 /50x.html;
                location = /50x.html {
                }
        }
}

特にHeaderは使ってない

忘れずにリスタート

sudo systemctl restart nginx

3. 確認方法

ルートにアクセスできることを確認

curl -i http://<YOUR_IP>

/adminには認証が要求されることを確認

curl -i http://<YOUR_IP>/admin/
curl -i -u <YOUR_NAME>:<YOUR_PASSWORD> http://<YOUR_IP>/admin/

エラーなどで困ったとき

nginx confのテスト
sudo nginx -t

ポートが立っているか
sudo lsof -i:80
sudo lsof -i:8080

認証の設定が反映されてなくて、積んだが、
/etc/nginx/nginx.confの設定が反映されてない
→include /etc/nginx/sites-enabled/*; をコメントアウトで解消

2
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?