5
4

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とapacheの共存

Posted at

参考URL:

nginxでリバースプロキシした際にIP情報を正しく取れるようにモジュールを追加

sudo yum install httpd-devel

cd /root/tmp
wget http://www.openinfo.co.uk/apache/extract_forwarded-2.0.2.tar.gz
tar zxf extract_forwarded-2.0.2.tar.gz
cd extract_forwarded
apxs -i -c -a mod_extract_forwarded.c

apacheの実行ユーザーとポートを変更

vim /etc/httpd/conf/httpd.conf
/etc/httpd/conf/httpd.conf
Listen 8080

#####
User nginx
Group nginx
#####
ServerName sample.jp:8080

# 最終行に追加
MEForder refuse,accept
MEFrefuse all
# Nginx(リバースプロキシ)のIPアドレス
MEFaccept 127.0.0.1


# ヴァーチャルホスト使っている場合はヴァーチャルホストのポートも変更

apacheを再起動して設定を反映

service httpd restart

nginxでの転送設定

cd /etc/nginx/conf.d
vim default.conf
default.conf
# デフォルトサーバー
server {
    listen       80;
    server_name  backend;

    # このサーバへの全てのアクセスを転送
    location / {
            proxy_pass http://127.0.0.1:8080;
            proxy_redirect                         off;

            # この設定がなくても.htaccessでの制限は可能。
            # ただし、cgi等から参照した際にNginxのIPアドレスになる。
            proxy_set_header Host                   $host;

            proxy_set_header X-Real-IP              $remote_addr;

            # 以下は、cgi等で明示的に利用していなければ、有効にする必要なし。
            proxy_set_header X-Forwarded-Host      $host;
            proxy_set_header X-Forwarded-Server    $host;

            # この設定がなくてもcgi等から正しいIPを確認可能。
            # ただし、.htaccessでの制限は不可。
            proxy_set_header X-Forwarded-For        $proxy_add_x_forwarded_for;
    }
}


最初に↓入れたほうがいいかも??

# キャッシュ関連設定
proxy_cache_path  /var/cache/nginx levels=1:2 keys_zone=czone:4m max_size=50m inactive=120m;
proxy_temp_path   /var/tmp/nginx;
proxy_cache_key   "$scheme://$host$request_uri";
proxy_set_header  Host               $host;
proxy_set_header  X-Real-IP          $remote_addr;
proxy_set_header  X-Forwarded-Host   $host;
proxy_set_header  X-Forwarded-Server $host;
proxy_set_header  X-Forwarded-For    $proxy_add_x_forwarded_for;

# バックエンドサーバ−(Apache)設定
upstream backend {
    # ip_hash;
    server 127.0.0.1:8080;
}

nginxを再起動して設定を反映

service nginx restart
5
4
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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?