歡迎您光臨本站 註冊首頁

nginx的安裝以及簡單擴展

←手機掃碼閱讀     火星人 @ 2014-03-29 , reply:0

Nginx配置

Nginx 是由 Igor Sysoev 為俄羅斯訪問量第二的 Rambler.ru 站點開發的,第一個公開版本0.1.0發佈於2004年10月4日。其將源代碼以類BSD許可證的形式發布,因它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名.

現在我就來教大家如何在RHEL6中配置nginx

參考網站:http://wiki.nginx.org/NginxChs


1.伺服器需要安裝的包

yum install pcre-devel openssl-devel gcc zlib-devel -y

2. 安裝
useradd nginx
tar zxf nginx-1.0.8.tar.gz
cd nginx-1.0.8
vi auto/cc/gcc
# debug
#CFLAGS=」$CFLAGS -g」 (註釋掉這行,去掉debug模式編譯,編譯以後程序只有幾百k)

vi src/core/nginx.h
#define NGINX_VERSION "1.0.6"
#define NGINX_VER "nginx" (修改此行,去掉後面的「NGINX_VERSION」,為了安全,這樣編譯
后外界無法獲取程序的版本號)

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
ln -s /usr/local/nginx/sbin/nginx /sbin3. 配置文檔
vi /usr/local/nginx/conf/nginx.conf
user nginx nginx; #使用的用戶和組
worker_processes 8; #指定工作衍生進程數
error_log logs/error.log info; #錯誤日誌定義類型
pid logs/nginx.pid; #指定 pid 存放的路徑

events {
use epoll; #使用高效網路I/O模型 具體內容查看 http:/wiki.codemongers.com/事件模型
worker_connections 1024; #允許的連接數
}
http {
include mime.types; #文件擴展名與文件類型映射表
default_type application/octet-stream; #默認文件類型
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' #日誌設定
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
server_names_hash_bucket_size 128; #伺服器名字的hash表大小
client_header_buffer_size 32k; #上傳文件大小限制
large_client_header_buffers 4 32k; #設定請求緩
client_max_body_size 8m; #設定請求緩

sendfile on; 開啟高效文件傳輸模式
tcp_nopush on; 防止網路阻塞
tcp_nodelay on; #防止網路阻塞
keepalive_timeout 65; 超時時間
gzip on;
gzip_min_length 1k; #最小壓縮文件大小
gzip_buffers 4 16k; #壓縮緩衝區
gzip_http_version 1.0; #壓縮版本(默認1.1,前端為squid2.5使用1.0)
gzip_comp_level 2; #壓縮等級
gzip_types text/plain application/x-javascript text/css application/xml; #壓縮類型
gzip_vary on;

server {
listen 80;
server_name localhost;

location / {
root html;
index index.html index.htm;
}

location /status { #設定查看Nginx狀態的地址
stub_status on;
access_log off;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

server {
listen 443;
server_name localhost;
ssl on;
ssl_certificate cert.pem; # (需要手工生成 make -C /etc/pki/tls/certs/cert.pem )
(mv /etc/pki/tls/certs/cert.pem /usr/local/nginx/conf)
ssl_certificate_key cert.pem;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location / {
root html;
index index.html index.htm;
}
}
4.配置cert.pem文件
cd /etc/pki/tls/certs/
make cert.pem
.......
Country Name (2 letter code) [XX]:ch #國家名稱(2字母代碼)
State or Province Name (full name) []:shannxi #州或省的名稱(全稱)
Locality Name (eg, city) [Default City]:xi'an #地區名稱(如,市)[默認城市]
Organization Name (eg, company) [Default Company Ltd]:123.org #默認有限公司組織的名稱(例如,公司)]
Organizational Unit Name (eg, section) []:linux #組織單位的名稱(例如,一節)
Common Name (eg, your name or your server's hostname) []:salim #通用名稱(例如,您的姓名或您的伺服器的主機名
Email Address []:root@localhost #電子郵件地址


cp /etc/pki/tls/certs/cert.pem /usr/local/nginx/conf/

5. 調試
nginx -t #查看語法
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful

6. 啟動
nginx
nginx -s reload 刷新

訪問:http://127.0.0.1 Welcome to nginx!

這樣您就能看到nginx畫面了


安裝完畢nginx后,我將會會大家進行一些擴展,其中最有用的是虛擬用戶,和負載均衡。

一 虛擬用戶

1.創建虛擬主機:
mkdir /usr/local/nginx/conf/virtualhost
cd /usr/local/nginx/conf/virtualhost
mkdir www.luoning.com
mkdir www.luoning.cn
2.新建默認頁:
cd www.luoning.com echo luoning.com > index.html
cd www.luoning.cn echo www.luoning.cn> index.html
3.添加解析:
vi /etc/hosts
192.168.0.27 www.luoning.com
192.168.0.27 www.luoning.cn
4.用ping檢測虛擬主機查看路徑
5.vi /usr/local/nginx/conf/nginx.conf

添加

server {
listen 80;
server_name www.luoning.cn;
location / {
root virtualhost/www.luoning.cn;
index index.html index.htm;
}
}

server {
listen 80;
server_name www.luoning.com;
location / {
root virtualhost/www.luoning.com;
index index.html index.htm;
}
}

nginx -t 調試查看語法

訪問: http://www.luoning.com
http://www.luoning.cn



二 .負載均衡

vi /usr/local/nginx/conf/nginx.conf
......
upstream hello {
server 192.168.0.1:80;
server 192.168.0.2:80;
}

server {
listen 80;
server_name www.luoning.com;
location / {
proxy_pass http://hello;
}
}

nginx -t 調試查看語法
nginx -s reload 刷新

添加解析:vi /etc/hosts
www.hello.org
用ping查看路徑: ping www.luoning.com

訪問路徑: http://www.luoning.com 查看添加主機是否可以相互轉換,可相互轉換表示負載均衡完成

[火星人 ] nginx的安裝以及簡單擴展已經有347次圍觀

http://coctec.com/docs/linux/show-post-199172.html