Home > Tiếng Việt > Hướng Dẫn (Tutorials) > Hướng dẫn thiết lập NGINX làm Proxy cho Apache

Hướng dẫn thiết lập NGINX làm Proxy cho Apache

Sự kết hợp giữa nginx và apache

Chuẩn bị 

Cài đầu đủ các kho epel và remi ,tham khảo ở link Click here

Vps đã cài Apache 

Tham khảo ở link Click here

Tiến hành 

Các bạn dùng lệnh netstat để kiểm tra các cổng đang mở

# netstat -tunpl

Kết quả trả về là http đang chạy qua cổng 80

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      17360/mysqld

tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1429/sshd
tcp        0      0 :::80                     :::*                        LISTEN      14324/httpd
tcp        0      0 :::22                       :::*                        LISTEN      1429/sshd

Bước 1: Chỉnh sửa file cấu hình của http

Ta truy cập vào file httpd.conf 

# nano /etc/httpd/conf/httpd.conf

Và tìm chữ Listen 80 

# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
#
#Listen 12.34.56.78:80
Listen 80

#
# Dynamic Shared Object (DSO) Support

Thay thế bằng chữ Listen 8080 để không cho http chảy cổng 80 nữa

# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
#
#Listen 12.34.56.78:80
Listen 8080

#
# Dynamic Shared Object (DSO) Support

Tiếp tục tìm chữ AllowOverride None

<Directory />
    Options FollowSymLinks
    AllowOverride None

</Directory>

Và sửa thành AllowOverride All

  <Directory />
    Options FollowSymLinks
    AllowOverride All
</Directory>

Khởi động lại dịch vụ httpd

# service httpd restart

Bước 2: Cài đặi và cấu hình nginx

Trước tiên ta cần phải thêm gói cài đặtt nginx bằng cách thêm file nginx.repo

# nano /etc/yum.repos.d/nginx.repo

Trong file nginx.repo tạo nội dung như sau

[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/

gpgcheck=0

enabled=1

Dùng lệnh yum cài đặt nginx
# yum install nginx -y

Khởi động dịch vụ 

# service nginx start

Cho dịch vụ khởi động cùng hệ thống

# chkconfig nginx on

Ta chỉnh sửa file cấu hình của nginx 

# nano /etc/nginx/conf.d/default.conf

Ta sẽ thêm những dòng xanh , và thêm dấu # đằng trước các dòng đỏ

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
    location / {
                proxy_pass   http://ip-vps:8080;
    }

    #location / {
     #   root   /usr/share/nginx/html;
      #  index  index.html index.htm;
   # }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    location ~ \.php$ {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;

        proxy_pass http://127.0.0.1:8080;
    }


    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;

Ta khởi động lại dịch vụ nginx

# service nginx restart

Test xem thành công chưa

Gõ lệnh sau để kiểm tra

# curl -I http://ip-vps/info.php

Nếu nội dung như sau là thành công

HTTP/1.1 200 OK
Server: nginx/1.9.12
Date: Sat, 12 Mar 2016 02:07:06 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.19

Dùng lệnh netstat để kiểm tra kết nối

# netstat -tunpl

Kết quả nginx chạy port 80 , http chạy port 8080

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      17360/mysqld

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      14562/nginx
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1429/sshd
tcp        0      0 :::8080                     :::*                        LISTEN      14324/httpd
tcp        0      0 :::22                       :::*                        LISTEN      1429/sshd