nginx: Reverse Proxy สำหรับ Apache PHP

Byphunsanit

nginx: Reverse Proxy สำหรับ Apache PHP

ทั้ง nginx และ apache สามารถที่จะ run PHP ได้เหมือนกันแต่ถนัดกันคนละด้าน อะแพชีจะเร็วในการทำงานกับ php-fpm มากกว่า ส่วน nginx จะถนัดในการทำงานกับไฟล์ที่ไม่ต้องประมวลผล css, html, javascript และสามารถให้บริการลูกค้าจำนวนมากได้มากกว่า ถึงถูกใช้ในการเป็น proxy ให้ apache

  1. ติดตั้ง nginx โดยคำสั่ง
    brew install nginx
  2. ดูการติดตั้งโดยใช้
    brew info nginx
    จะเห็น message กลับมา เช่น
    ==> nginx: stable 1.27.0 (bottled), HEAD
    HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
    https://nginx.org/
    Installed
    /opt/homebrew/Cellar/nginx/1.27.0 (27 files, 2.4MB) *
    Poured from bottle using the formulae.brew.sh API on 2024-07-20 at 19:38:15
    From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/n/nginx.rb
    License: BSD-2-Clause
    ==> Dependencies
    Required: openssl@3 ✔, pcre2 ✔
    ==> Options
    –HEAD
    Install HEAD version
    ==> Caveats
    Docroot is: /opt/homebrew/var/www
    The default port has been set in /opt/homebrew/etc/nginx/nginx.conf to 8080 so that
    nginx can run without sudo.
    nginx will load all files in /opt/homebrew/etc/nginx/servers/.
    To restart nginx after an upgrade:
    brew services restart nginx
    Or, if you don’t want/need a background service you can just run:
    /opt/homebrew/opt/nginx/bin/nginx -g daemon\ off\;
    ==> Analytics
    install: 12,297 (30 days), 43,162 (90 days), 162,914 (365 days)
    install-on-request: 12,264 (30 days), 42,989 (90 days), 162,428 (365 days)
    build-error: 13 (30 days)
    จะเห็นข้อมูลสำคัญ 3 จุดคือ
    • เวอร์ชั่นที่ติดตั้งคือ 1.27.0
    • document root จะอยู่ที่ Docroot is: /opt/homebrew/var/www
    • ตัวไฟล์ config จะอยู่ที่ /opt/homebrew/etc/nginx/nginx.conf
  3. เปิดไฟล์ config ขึ้นมาเช่น
    nano /opt/homebrew/etc/nginx/nginx.conf
    มองหา
    1. user nobody;
      เปลี่ยนเป็น
      #user nobody;
      user _www;
      เพื่อใช้ user เดี่ยวกับ Apache
    2. listen 8080;
      เปลี่ยนเป็น
      #listen 8080;
      listen 80;
      listen [::]:80;
      เพราะว่าต้องการเปลี่ยนไปใช้ port ปกติของ web และ port 8080 ถูกใช้โดย apache อยู่แล้ว
    3. server_name localhost;
      เปลี่ยนเป็น
      #server_name localhost;
      server_name _;
    4. location / {
      root html;
      index index.html index.htm;
      }
      เพิ่ม
      try_files $uri $uri/ /index.php;
      เพื่อถ้าไม่ระบุไฟล์ ให้ลองถามหา index.php ดู
    5. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
      #
      #location ~ \.php$ {
      # proxy_pass http://127.0.0.1;
      #}
      เปลี่ยนเป็น
      # proxy the PHP scripts to Apache listening on 127.0.0.1:80
      location ~ \.php$ {
      proxy_pass http://127.0.0.1:8080;
      proxy_set_header Host $http_host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Real-IP $remote_addr;
      }
      เป็นการส่งพีเอชพีไป apache ทำงานแทนที่ 127.0.0.1 port 8080
    6. เพิ่มใน server {

      }
      เพื่อป้องกัน user โหลดไฟล์ .htaccess และ .htpasswd ที่มีข้อมูลที่ไม่ปลอดภัยอยู่ได้
      # Deny access to .htaccess, .htpassword
      location ~ /\.ht {
      deny all;
      }
  4. เทสว่าเขียน configuration ถูกมั๋ยโดยใช้คำสั่ง
    nginx -T
    ถ้าไม่มีแจ้ง error กลับมาก็ใช้ได้
  5. restart nginx โดยคำสั่ง
    brew services restart naginx
  6. ทดสอบโดยการเรียก
    http://localhost/phpinfo.php
    ถ้าเปิด developer mode
    • Server ควรจะเป็น nginx/1.27.0
    ใน phpinfo
    • SERVER_SOFTWARE ควรจะเป็น Apache/2.4.62 (Unix) PHP/8.3.9
      ใน phpinfo
  7. ปิด firewall ไม่ใช้ apache สามารถเรียกจากด้านนอกโดย port 80, 8080

เท่านี้ก็ใช้ความสามารถของ apache, nginx และ PHP ได้อย่างเต็มที่

About the author

phunsanit administrator