ป้ายกำกับ: Apache

LAMP + LEMP = LAEMP (nginx as reverse proxy )LAMP + LEMP = LAEMP (nginx as reverse proxy )

จากการเลือกเซิร์ฟเวอร์ Apache และ Nginx LAMP VS LEMP ที่มีทางออกที่ 3 คือ เอาข้อดีของทั้งสองมาใช้คู่กัน ทำตัวอย่างการ config Apache และ Nginx มาใช้ด้วยกัน เรียก เทคนิคนี้ว่า nginx as reverse proxy ทำได้โดยการเซ็ต
Virtual directory ตามตัวอย่าง

/etc/apache2/sites-available/pitt.plusmagi.com.conf

<VirtualHost *:80>
 <Directory /usr/share/www/pitt.plusmagi.com/published>
 #directive needed in Apache 2.4:
 Require all granted

 AllowOverride All
 Options FollowSymLinks MultiViews
 </Directory>
 DefaultLanguage th_TH
 DocumentRoot /usr/share/www/pitt.plusmagi.com/published

 #Log files
 CustomLog ${APACHE_LOG_DIR}/pitt.plusmagi.com.access.log combined
 ErrorLog ${APACHE_LOG_DIR}/pitt.plusmagi.com.error.log

 #rewrite
 RewriteEngine On
 RewriteCond %{HTTPS} !=on
 RewriteRule ^/? (.*) https://%{SERVER_NAME}/$1 [R=301,L]

 #security
	php_admin_value open_basedir "/usr/share/www/pitt.plusmagi.com/published"
 
 #server
 ServerAdmin [email protected]
 ServerAlias www.plusmagi.com
 ServerName pitt.plusmagi.com
</VirtualHost>

/etc/nginx/sites-available/pitt.plusmagi.com.conf

server {
 http2 on;
 http3 on;

 server_name pitt.plusmagi.com;

 location ~ /\.ht {
 deny all;
 }

 location ~ \.php$ {
 proxy_pass http://127.0.0.1:8080;
 proxy_set_header Host $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;
 }

 location ~* \. (css|js|jpg|jpeg|png|gif|ico|svg|woff|woff2|ttf|eot) $ {
 access_log off;
 add_header Cache-Control "public";
 expires max;
 root /usr/share/www/pitt.plusmagi.com/published;
 }

 # Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;

 location / {
 try_files $uri $uri/ /index.php;
 }
}

จาก config ทั้ง 2 ไฟล์ หลักการคือ ให้แต่ละ server ทำสิ่งที่ตัวเองถนัด คือ Apache จะเหมาะกับการทำงานในส่วน PHP และตัว Nginx เหมาะกับการที่ใช้ส่ง static files ที่จะไม่ต่างกันในแต่ละ user

ทดสอบได้โดยการ restart server และดูด้วยคำสั่ง sudo lsof -i :80 และ sudo lsof -i :8080

ถ้าต้องการ คน set server ให้ทำแบบนี้ ทักมาได้ครับ

อ่านเพิ่มเติม