Nginx(發音同 engine x)是一款由俄羅斯程式設計師 Igor Sysoev 所開發輕量級的網頁伺服器、反向代理伺服器以及電子郵件(IMAP/POP3)代理伺服器。起初是供俄國大型的入口網站及搜尋引擎 Rambler(俄語:Рамблер)使用。此軟體 BSD-like 協定下發行,可以在 UNIX、GNU/Linux、BSD、Mac OS X、Solaris,以及 Microsoft Windows等作業系統中執行。設定 yum
# vi /etc/yum.repos.d/nginx.repo
# vi /etc/yum.repos.d/nginx.repo
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1
使用 yum 安裝 nginx
# yum install nginx Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror01.idc.hinet.net * extras: mirror01.idc.hinet.net * updates: mirror01.idc.hinet.net nginx | 2.9 kB 00:00 nginx/primary_db | 24 kB 00:00 Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package nginx.x86_64 0:1.4.2-1.el6.ngx will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================================================================================================================= Package Arch Version Repository Size ================================================================================================================================================================================= Installing: nginx x86_64 1.4.2-1.el6.ngx nginx 311 k Transaction Summary ================================================================================================================================================================================= Install 1 Package(s) Total download size: 311 k Installed size: 770 k Is this ok [y/N]: y Downloading Packages: nginx-1.4.2-1.el6.ngx.x86_64.rpm | 311 kB 00:01 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : nginx-1.4.2-1.el6.ngx.x86_64 1/1 ---------------------------------------------------------------------- Thanks for using NGINX! Check out our community web site: * http://nginx.org/en/support.html If you have questions about commercial support for NGINX please visit: * http://www.nginx.com/support.html ---------------------------------------------------------------------- Verifying : nginx-1.4.2-1.el6.ngx.x86_64 1/1 Installed: nginx.x86_64 0:1.4.2-1.el6.ngx Complete!
建立 nginx所使用 的 cache 目錄
mkdir /var/nginx mkdir /var/nginx/cache
設定 nginx.conf
其中 proxy_pass http://xxx.xxx.xxx.xxx; 要設定你要指向的 proxy ip
# vi /etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
proxy_cache_path /var/nginx/cache levels=1:2 keys_zone=STATIC:10m
inactive=24h max_size=1g;
client_max_body_size 10G;
server {
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://xxx.xxx.xxx.xxx;
proxy_set_header Host $host;
proxy_cache STATIC;
proxy_cache_valid 200 1d;
proxy_cache_use_stale error timeout invalid_header updating
http_500 http_502 http_503 http_504;
}
}
include /etc/nginx/conf.d/*.conf;
}
如果要對應多站台的話
# vi /etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
proxy_cache_path /var/nginx/cache levels=1:2 keys_zone=STATIC:10m
inactive=24h max_size=1g;
client_max_body_size 10G;
sendfile on;
tcp_nopush on;
keepalive_timeout 600;
tcp_nodelay on;
upstream backend {
server {your-backend-server1}:80;
server {your-backend-server2}:80;
}
server {
location / {
add_header X-Proxy-Cache $upstream_cache_status;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_cache_key "$scheme$host$request_uri";
proxy_cache STATIC;
proxy_cache_valid 200 1d;
proxy_cache_use_stale error timeout invalid_header updating
http_500 http_502 http_503 http_504;
proxy_connect_timeout 6000;
proxy_send_timeout 6000;
proxy_read_timeout 6000;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
send_timeout 6000;
proxy_buffering off;
}
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";
}
include /etc/nginx/conf.d/*.conf;
}
啟動服務並設為自動啟動
# service nginx restart # chkconfig nginx on
ubuntu 安裝 nginx
sudo apt update sudo apt install nginx



