Crmgo Blog

Nginx on CentOS

Nginx on CentOS

yum install nginx Now let's reconfigure Apache. To do this, open /etc/httpd/conf/httpd.conf and change NameVirtualHost to: NameVirtualHost 127.0.0.1:8080. Also, change the opening tag of each virtual host to: <VirtualHost 127.0.0.1:8080>. That's it for Apache. Now go to /etc/nginx and open nginx.conf. So,...

Nginx on CentOS
yum install nginx

Now let's reconfigure Apache. To do this, open /etc/httpd/conf/httpd.conf and change NameVirtualHost to:

NameVirtualHost 127.0.0.1:8080

Also, change the opening tag of each virtual host:

<VirtualHost 127.0.0.1:8080>

That's it for Apache. Now go to /etc/nginx and open nginx.conf. Here are the general parameters:

user nginx; worker_processes 4; error_log /var/log/nginx/error.log debug; pid /var/run/nginx.pid;
events { worker_connections 1024; }

HTTP server parameters:

http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] $status ' '"$request" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; #Content archiving gzip on; gzip_min_length 1100; gzip_proxied expired no-cache no-store private auth; gzip_types text/plain application/xml application/x-javascript text/javascript text/css; gzip_disable "msie6"; gzip_disable Firefox/([0-2]\.|3\.0); gzip_disable Chrome/2; gzip_disable Safari; gzip_comp_level 3;

And now, for each Virtual Host, the following record should be present:

server { listen 80; server_name eddnet.org www.eddnet.org; access_log /var/log/nginx/host.access.log main; location / { proxy_pass http://127.0.0.1:8080/; 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_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 10m; }

If you want to cache some content, simply add the following to the description:

location /uploads/files { add_header Content-type application/octet-stream; internal; } location /uploads/img { #Cache images for 4 months expires 4M; #Cache everywhere (both on the proxy and on clients) add_header Cache-Control public; } location /cache/tumb/ { #Cache images for 4 months expires 4M; #Cache everywhere (both on the proxy and on clients) add_header Cache-Control public; }

Now here is the full configuration, so you don't get confused about what goes where and how:

user nginx; worker_processes 4; error_log /var/log/nginx/error.log debug; pid /var/run/nginx.pid; events { worker_connections 1024; } ## Timeouts http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] $status ' '"$request" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; gzip on; gzip_min_length 1100; gzip_proxied expired no-cache no-store private auth; gzip_types text/plain application/xml application/x-javascript text/javascript text/css; gzip_disable "msie6"; gzip_disable Firefox/([0-2]\.|3\.0); gzip_disable Chrome/2; gzip_disable Safari; gzip_comp_level 3; server { listen 80; server_name eddnet.org www.eddnet.org; access_log /var/log/nginx/host.access.log main; location / { proxy_pass http://127.0.0.1:8080/; 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_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 10m; } } server { listen 80; server_name wot-news.com www.wot-news.com; access_log /var/log/nginx/host.access.log main; root /var/www/vhosts/wot-news.com/http; expires epoch; location / { proxy_pass http://127.0.0.1:8080/; 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_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 10m; } location /uploads/files { add_header Content-type application/octet-stream; internal; } location /uploads/img { # Cache images for 4 months expires 4M; # Cache everywhere (both on proxies and clients) add_header Cache-Control public; } location /cache/tumb/ { # Cache images for 4 months expires 4M; # Cache everywhere (both on proxies and clients) add_header Cache-Control public; } } }

Note: This post may be AI-assisted and then human-edited. Editorial responsibility: Dmytro Lomaka, Kaltenkirchen.