CẤU HÌNH NGINX LÀM REVERSE PROXY KẾT HỢP VỚI WEB SERVER APACHE

0
2986
Mô Hình :    Client <—> Nginx<—->Apache

Nginx và Apache chạy trên cùng một server ở hai port dịch vụ khác nhau. Cụ thể Nginx sẽ chịu trách nhiệm lắng nghe các request từ client và gởi cho Apache==>cấu hình Nginx chạy port 80. Apache chỉ nhận request cục bộ từ Nginx và trả lời lại, nên port là tùy chọn (đừng xung đột với các dịch vụ đang chạy khác..). Ở đây mình chọn port 8080 (socket 127.0.0.1:8080)

 

Cài đặt LAMP

Sử dụng script cài đặt LAMP tự động:

https://ductam.info/script-cai-dat-lamp-tu-dong/

Sửa cấu hình apache
vi /etc/httpd/conf/httpd.conf

Tìm dòng khai báo Listen và sửa lại thành

Listen 127.0.0.1:8080

Tạo một Vhost, ví dụ ở đây là test.vn

<VirtualHost 127.0.0.1:8080>
DocumentRoot "/home/test.vn"
ServerName test.vn
DirectoryIndex index.php index.html
</VirtualHost>

Cài đặt nginx

yum install nginx

Sửa cấu hình nginx:

vi /etc/nginx/nginx.conf

Nội dung:

server {
 listen 80;
server_name test.vn www.test.vn;
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer"   '"$http_user_agent" "$http_x_forwarded_for"';
access_log  /var/log/nginx/test.vn-access.log  main;
location / {
 include proxy.conf;
 proxy_pass http://127.0.0.1:8080;
 proxy_cache my-cache;
 proxy_cache_valid  200 302  60m;
 proxy_cache_valid  404      1m;
}

Tạo file proxy.conf với các thông số sau (đặt file này ngang hàng với nginx.conf):

vi /etc/nginx/proxy.conf

Nội dung:

proxy_redirect          off;
proxy_set_header        Host            $host;
proxy_set_header        X-Real-IP       $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffers           32 4k;

Tiếp theo chúng ta khai báo đường dẫn lưu cache và các tùy chọn khác

vi /etc/nginx/nginx.conf

Thêm vào sau http { các thông sô cache sau:

proxy_cache_path  /tmp/cache levels=1:2 keys_zone=my-cache:8m max_size=1000m inactive=600m;
proxy_temp_path /tmp/cache/tmp;

Tạo thư mục chứa các file cache

mkdir /tmp/cache
mkdir /tmp/cache/tmp
chown -R nginx.nginx /tmp/cache
chmod 700 /tmp/cache

 

(Ở đây, Nginx của mình chạy với user nginx, nên mình cấp quyền cho user này ở thư mục trên)

Thực hiện restart lại nginx và httpd và thử truy cập để xem kết quả

KIỂM TRA

Tạo file info.php và đặt trong /home/test.vn
Nội dung info.php

<?php

// Show all information, defaults to INFO_ALL
phpinfo();

// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo(INFO_MODULES);

?>

Truy cập : http://IP/info.php

Tiếp tục thực hiện lệnh :

curl -I http://IP/info.php

Kết quả trả về :

[root@localhost tmp]# curl -I http://192.168.1.210/info.php
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 16 Jul 2019 02:37:26 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.2.20

 


Leave a Reply

avatar
  Subscribe  
Notify of