yum install pcre-devel -y
yum install openssl-devel -yhtml
tar zxvf nginx-1.0.2.tar.gzmysql
優化安裝nginx
cd nginx-1.0.8sql
vi auto/cc/gcc apache
#CFLAGS=」$CFLAGS -g」 (註釋掉這行,去掉debug模式編譯,編譯之後程序只有幾百k)安全
vi src/core/nginx.h
#define NGINX_VERSION "1.0.2」
#define NGINX_VER "nginx" (修改此行,去掉後面的「NGINX_VERSION」,爲了安全,這樣編譯後
外界沒法獲取程序的版本號)bash
useradd -M -s /sbin/nologin nginxsession
./configure --prefix=/usr/local/lnmp/nginx --user=nginx --with-http_stub_status_module --withhttp_
ssl_module
make && make install負載均衡
vi /usr/local/lnmp/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 1;
events {
use epoll; /wiki.nginx.org/ 能夠查到調優參數
worker_connections 1024;ide
}
server {
listen 80;
server_name desktop144.example.com;
/usr/local/lnmp/nginx/sbin/nginx 啓動程序
vi ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/nginx/sbin
nginx -t 檢測語法
nginx 運行
nginx -s reload stop
nginx 加密
vi nginx.conf
server {
listen 443;
server_name desktop144.example.com;
ssl on;
ssl_certificate cert.pem; 同時指定pem
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;
}
}
在 /etc/pki/tls/certs下建立證書
mv cert.pem /usr/local/lnmp/nginx/conf/ 將證書放到指定位置
虛擬主機 編輯nginx主配置文件nginx.conf
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; 指定日誌main 取消"#"
server {
listen 80;
server_name www.example1.com;
access_log logs/example2.access.log main;
location / {
index index.html;
root html/example1.com;
}
}
server {
listen 80;
server_name www.example2.com;
access_log logs/example2.access.log main;
location / {
index index.html;
root html/example2.com;
}
}
負載均衡
主ip192.168.0.144
輔助2apache 192.168.0.66 192.168.0.126 爲方便輔助就用apache了
vi nginx.conf
http {
upstream myproject {
server 192.168.0.66:80 weight=3; 權重:連續3次後切換126
server 192.168.0.126:80;
}
server {
listen 80;
server_name www.example.com;
location / {
proxy_pass http://myproject;
}
vi /etc/hosts
192.168.0.144 www.example.com 在客戶機上添加主機名 能夠訪問