Welcome! Log In Create A New Profile

Advanced

Menggunakan nginx untuk mempercepat Apache di Cpanel server

Posted by omrapid 
Menggunakan nginx untuk mempercepat Apache di Cpanel server
December 05, 2010 04:59PM
Nginx - tersebut, kecil kilat cepat dan server web yang sangat efisien biasanya digunakan untuk melayani konten statis atau sebagai reverse proxy / penyeimbang beban untuk Apache atau backends relatif lambat lainnya. Jadi itu akan alami untuk menggunakan nginx sebagai tampilan untuk Cpanel's Apache. Ini akan menghemat sejumlah besar memori dan waktu CPU biasanya terisap oleh anak-anak Apache spoonfeeding banyak konten kepada klien.

Saya selalu memiliki pemikiran ini, tetapi sampai saat ini tidak punya waktu untuk melihat secara dekat mengimplementasikannya. Lalu aku melihat sebuah posting forum dengan contoh script untuk menghasilkan file nginx konfigurasi berdasarkan informasi account Cpanel, dan kemudian sebuah serangan pengunjung pada server Cpanel bersama saya admin melambat untuk merangkak, dan aku dipaksa untuk menyelidiki jeroan dari Cpanel. Sebagai hasil dari penyelidikan ini saya menulis "nginx di Cpanel" HOWTO yang disajikan di bawah ini.

[b]Instalasi Apache modul[/b]

Pertama-tama, ketika nginx digunakan sebagai proxy reverse ke Apache, IP pengunjung 'yang diterima oleh Apache adalah salah - semua permintaan untuk Apache berasal dari nginx, sehingga server utama IP akan mulai bisa login.
Untuk membuat Apache log IP nyata dari pengunjung bukan IP server utama, sebuah modul Apache khusus (mod_rpaf) diperlukan.
Download, untar, cd ke direktori yang baru dibuat dan jalankan perintah ini sebagai root:
/ Usr / local / apache / bin / apxs-i-c-n mod_rpaf-2.0.so mod_rpaf-2.0.c
Yang akan menginstal modul ke direktori modul Apache.

Lalu pergi ke WHM, Utama>> Layanan Konfigurasi>> Apache Konfigurasi> Termasuk Editor> Pra Utama Sertakan dan tambahkan bagian ini di sana, menggantikan LIST_OF_YOUR_IPS dengan daftar alamat IP yang dikelola oleh Cpanel:

LoadModule rpaf_module modules/mod_rpaf-2.0.so

[code]
RPAFenable On
# Enable reverse proxy add forward
RPAFproxy_ips 127.0.0.1 LIST_OF_YOUR_IPS
# which ips are forwarding requests to us
RPAFsethostname On
# let rpaf update vhost settings
# allows to have the same hostnames as in the "real"
# configuration for the forwarding Apache
RPAFheader X-Real-IP
# Allows you to change which header mod_rpaf looks
# for when trying to find the ip the that is forwarding
# our requests
[/code]

[b]Perubahan konfigurasi apache[/b]

Maka kita perlu untuk memindahkan Apache untuk port lain, mari kita 81 misalnya. Anda hanya dapat mengeditnya di halaman "Pengaturan Tweak" di WHM, menggantikan 0.0.0.0:80 dengan 0.0.0.0:81 atau, melakukan dengan cara command line, edit / var / cpanel / cpanel.config dan perubahan port 80 di apache_port 81 penugasan:

apache_port = 0.0.0.0:81

Jalankan / usr/local/cpanel/whostmgr/bin/whostmgr2 - updatetweaksettings sebagai disarankan di bagian atas file tersebut.
Periksa / usr / local / apache / conf / httpd.conf untuk setiap kejadian port 80, lalu jalankan / scripts / rebuildhttpdconf untuk membuat httpd.conf yakin up to date.

Hal ini juga masuk akal untuk mengurangi jumlah anak Apache, sebagai nginx akan mengurus spoonfeeding data untuk menghubungkan klien melalui link jaringan yang lambat, membebaskan anak-anak Apache untuk melakukan pekerjaan backend mereka. Edit / usr / local / apache / conf / httpd.conf dan ganti bagian prefork.c dengan ini (catatan yang saya gunakan nilai-nilai yang sangat sederhana di sini, dan jarak tempuh Anda mungkin berbeda):

[code]
<IfModule prefork.c>
StartServers 5
MinSpareServers 2
MaxSpareServers 5
MaxClients 50
MaxRequestsPerChild 0
</IfModule>
[/code]

Run /usr/local/cpanel/bin/apache_conf_distiller --update --main to pick up the changes, and then /scripts/rebuildhttpdconf to make sure your changes are in.
Note that you will need to watch Apache extended server status at the peak load times to have an idea how many Apache children your server needs by default.

Anda juga harus update Apache port di /etc/chkserv.d/httpd dan restart chksrvd with /etc/init.d/chksrvd restart

[b]Generating nginx config files[/b]

Langkah terakhir - kita harus membangun nginx file konfigurasi berdasarkan domain host pada server Anda.
Hal ini dilakukan oleh script sederhana yang akan menghasilkan file konfigurasi dua untuk nginx - satu utama di sini: [code]
/usr/local/nginx/conf/nginx.conf
[/code]


dan semua virtual hosts: [code]
/usr/local/nginx/conf/vhost.conf
[/code]

[code]
#!/bin/sh

cat > "/usr/local/nginx/conf/nginx.conf" <<EOF
user nobody;
# no need for more workers in the proxy mode
worker_processes 1;

error_log logs/error.log info;

worker_rlimit_nofile 8192;

events {
worker_connections 512; # increase for more busy servers
use rtsig; # you should use epoll here for Linux kernels 2.6.x
}

http {
server_names_hash_max_size 2048;

include mime.types;
default_type application/octet-stream;

sendfile on;
tcp_nopush on;
tcp_nodelay on;

keepalive_timeout 10;

gzip on;
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_types text/plain text/html application/x-javascript text/xml text/css;
ignore_invalid_headers on;

client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;

include "/usr/local/nginx/conf/vhost.conf";
}

EOF

/bin/cp /dev/null /usr/local/nginx/conf/vhost.conf

cd /var/cpanel/users
for USER in *; do
for DOMAIN in `cat $USER | grep ^DNS | cut -d= -f2`; do
IP=`cat $USER|grep ^IP|cut -d= -f2`;
ROOT=`grep ^$USER: /etc/passwd|cut -d: -f6`;
echo "Converting $DOMAIN for $USER";

cat >> "/usr/local/nginx/conf/vhost.conf" <<EOF
server {
access_log off;

error_log logs/vhost-error_log warn;
listen 80;
server_name $DOMAIN www.$DOMAIN;

# uncomment location below to make nginx serve static files instead of Apache
# !WARNING!
# it will make the bandwidth accounting incorrect as these files won't be logged!
#location ~* \.(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|htm|html|js|css)$ {
# root $ROOT/public_html;
#}

location / {
client_max_body_size 10m;
client_body_buffer_size 128k;

proxy_send_timeout 90;
proxy_read_timeout 90;

proxy_buffer_size 4k;
# you can increase proxy_buffers here to suppress "an upstream response
# is buffered to a temporary file" warning
proxy_buffers 16 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

proxy_connect_timeout 30s;

proxy_redirect http://www.$DOMAIN:81 http://www.$DOMAIN;
proxy_redirect http://$DOMAIN:81 http://$DOMAIN;

proxy_pass http://$IP:81/;

proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
}
}
EOF
done
done
[/code]

Jalankan /usr/local/nginx/sbin/nginx -t

untuk memeriksa configuration, kemudian /usr/local/nginx/sbin/nginx untuk memulai nginx. Selesai dech...

Indahnya dunia dengan berbagi ilmu pengetahuan
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 162
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready