Nginx 安裝筆記(含PHP支持、虛擬主機、反向代理負載均衡)
系統(tǒng)環(huán)境:RHEL5 [ 2.6.18-8.el5xen ]
軟件環(huán)境:
nginx-0.7.17
lighttpd-1.4.20.tar.gz
pcre-6.6-1.1
pcre-devel-6.6-1.1
php-5.1.6-5.el5
參考下載地址:
http://sysoev.ru/nginx/nginx-0.7.17.tar.gz (最新穩(wěn)定版為0.6.32)
http://www.lighttpd.net/download/lighttpd-1.4.20.tar.gz
##########################################################################
一、安裝支持軟件
1、安裝lighttpd以提取spawn-fcgi (如果站點不包含php頁面,可以不安裝spaw-fcgi、PHP)
shell> tar zxvf lighttpd-1.4.20.tar.gz
shell> cd lighttpd-1.4.20/
shell> ./configure && make
shell> cp -p src/spawn-fcgi /usr/sbin/spawn-fcgi
2、安裝pcre和php(以下軟件)
可使用RHEL5自帶的rpm包安裝,過程略。
二、安裝nginx
shell> tar zxvf nginx-0.7.17.tar.gz
shell> cd nginx-0.7.17/
shell> ./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module
shell> make && make install
shell> ln -sf /opt/nginx/sbin/nginx /usr/sbin/
三、nginx運行控制
1、檢查配置文件有無語法錯誤
shell> nginx -t
2、啟動(不帶任何參數(shù)直接運行即可)
shell> nginx
3、重新加載nginx配置
shell> killall -s HUP nginx #//或者 killall -1 nginx
4、處理完當前請求后退出nginx
shell> killall -s QUIT nginx #//或者 killall -3 nginx
四、nginx配置用例
1、常規(guī)配置
shell> vi /opt/nginx/conf/nginx.conf
worker_processes 1; #//工作進程數(shù)
events {
use epoll; #//增加該事件提高I/O性能
work_connections 4096;
}
http {
include mime.types;
default_types application/octet-stream;
sendfile on;
tcp_nodelay on
keepalive_timeout 60;
server {
listen 80; #//設置監(jiān)聽端口,注意不要和Apache等其他Web程序沖突
server_name www.linux.org; #//指定使用的主機名
charset utf-8; #//指定站點文件的默認編碼
location / {
root html; #//設置網(wǎng)站根目錄
index index.html index.html;
}
error_page 500 502 503 504 /50x.html
location = /50x.html {
root html;
}
}
}
2、添加狀態(tài)監(jiān)控
shell> vi /opt/nginx/conf/nginx.conf #//增加以下內(nèi)容
location ~ ^/NginxStatus/ {
stub_status on;
access_log off;
}
shell> killall -1 nginx
#//使用瀏覽器訪問 http://nginx_server_ip/NginxStatus/ 即可看到狀態(tài)統(tǒng)計頁面。(三個數(shù)字分別表示:總共處理連接數(shù)、成功創(chuàng)建的握手次數(shù)、總共處理的請求數(shù))
3、通過FastCGI方式支持PHP語言
1)啟動FastCGI服務(用php-cgi做實際處理php頁面的程序,用spawn-fcgi是便于同時開啟多個php-cgi進程——“-C”選項控制子進程數(shù))
shell>/usr/sbin/spawn-fcgi -a 127.0.0.1 -p 9000 -f /usr/bin/php-cgi -C 10
2)修改/opt/nginx/conf/nginx.conf配置文件,添加以下內(nèi)容:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
3)重新加載配置
shell> killall -1 nginx
4、虛擬主機設置
修改nginx.conf文件,增加一個server {……}配置即可,每個虛擬主機的參數(shù)可以獨立配置。
http {
server {
listen 80;
server_name www.vhost1.com;
access_log logs/vhost1.access.log main;
location / {
index index.html;
root /var/www/vhost1; #//第1個虛擬主機的網(wǎng)頁根目錄
}
}
server {
listen 80;
server_name www.vhost2.com;
access_log logs/vhost2.access.log main;
location / {
index index.html;
root /var/www/vhost2; #//第2個虛擬主機的網(wǎng)頁根目錄
}
}
}
5、基于反向代理的負載均衡
修改nginx.conf文件,增加upstream配置,指定對應服務器群的IP和權(quán)重,并調(diào)整server段中的網(wǎng)頁根目錄配置。使訪問nginx服務器的HTTP請求分散到Web群集中的服務器來處理。
http {
upstream my_web_cluster {
server 192.168.2.11:8000 weight=3;
server 192.168.2.12:8000 weight=3;
server 192.168.2.13:8000 weight=3;
server 192.168.2.14:8000 weight=3;
server 192.168.2.15:8000 weight=3;
}
server {
listen 80;
server_name www.domain.com;
location / {
proxy_pass http://my_web_cluster;
proxy_set_header x-real-IP $remote_addr;
}
#//注:其他的location配置段(如關(guān)于.php文件的)需注釋掉,否則可能影響該類文件的重定向。
}
}
相關(guān)文章
nginx中的proxy_set_header參數(shù)指令詳解
本文介紹了Nginx中的proxy_set_header指令,用于自定義代理請求的HTTP頭部信息,實現(xiàn)更靈活的反向代理功能,提供了實際應用場景和配置示例,幫助讀者更好地理解和使用proxy_set_header指令,感興趣的朋友一起看看吧2025-03-03
Ubuntu?22.04.1?LTS?編譯安裝?nginx-1.22.1的配置過程
Ubuntu安裝Nginx有兩種方式,一種是通過命令的方式,這種方式安裝的Nginx版本低,之前漏掃掃出來Nginx版本低,需要升級所以現(xiàn)在用編譯的方式安裝版本高點的,本文介紹Ubuntu22.04.1?LTS編譯安裝nginx1.22.1的配置過程,本文給大家介紹的非常詳細,需要的朋友參考下吧2024-01-01
查看nginx是否已經(jīng)啟動的幾種方法總結(jié)
在Ubuntu或其他Linux系統(tǒng)上,有多種方法可以檢查Nginx是否已經(jīng)啟動,文中通過代碼介紹的非常詳細,對大家學習或者使用nginx具有一定的參考借鑒價值,需要的朋友可以參考下2024-11-11
Nginx 實現(xiàn)灰度發(fā)布的三種方法總結(jié)
這篇文章主要介紹了Nginx 實現(xiàn)灰度發(fā)布的三種方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-05-05

