Nginx安裝
Nginx 是什么
Nginx 是輕量級 開源 穩定 高并發 的HTTP服務器和代理服務器
主要用作我們的圖片等靜態文件服務器和webserver的代理服務器
安裝步驟
下載相關依賴
yum install gcc-c++
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl—devel
進入到準備安裝目錄
cd /usr/local
下載nginx:
wget http://nginx.org/download/nginx-1.9.9.tar.gz
解壓nginx到當前目錄
tar -zvxf nginx-1.9.9.tar.gz
添加用戶和用戶目錄:
groupadd www
useradd -g www www -s /sbin/nologin
mkdir -p /data/www
chmod +w /data/www
chown -R www:www /data/www
進入nginx 目錄:
cd nginx-1.9.9/
執行:
./configure --user=www --group=www /** 默認安裝在/usr/local/nginx */
make
make install
nginx centos 開機啟動
編輯開機啟動文件
vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
Systemctl 用法
systemctl is-enabled nginx.service #查詢服務是否開機啟動
systemctl enable nginx.service #開機運行服務
systemctl disable nginx.service #取消開機運行
systemctl start nginx service #啟動服務
systemctl stop nginx.service #停止服務
systemctl restart nginx.service #重啟服務
systemctl reload nginx.service #重新加載服務配置文件
systemctl status nginx.service #查詢服務運行狀態
systemctl --failed #顯示啟動失敗的服務
Nginx 詳細配置說明
日志文件配置
詳細見:
error_log logs/error.log info;
access_log logs/access.log main;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
1.$remote_addr 與$http_x_forwarded_for 用以記錄客戶端的ip地址;
2.$remote_user :用來記錄客戶端用戶名稱;
3.$time_local : 用來記錄訪問時間與時區;
4.$request : 用來記錄請求的url與http協議;
5.$status : 用來記錄請求狀態;成功是200,
6.$body_bytes_s ent :記錄發送給客戶端文件主體內容大小;
7.$http_referer :用來記錄從那個頁面鏈接訪問過來的;
8.$http_user_agent :記錄客戶端瀏覽器的相關信息;
每個server 可以配置獨立的文件 獨立分析
配置合適的分隔符方便日志文件分析
日志統一使用##分割方便日后分析
nginx緩存配置
不配置緩存
nginx 反向代理配置
詳見測試例子:
upstream dev.zzc {
server 192.168.1.223:8081;
ip_hash;
}
upstream test.zzc {
server 192.168.1.223:18080;
ip_hash;
}
upstream online.zzc {
server 192.168.1.223:8085;
ip_hash;
}
server {
listen 80;
server_name test.zzc;
location /SCM/ {
proxy_pass http://test.zzc;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
index /;
}
location /images/ {
alias /apps/filebase/test/images/;
}
location /resources/ {
alias /apps/filebase/test/resources/;
}
}
server {
listen 81;
server_name dev.zzc;
location /SCM/ {
proxy_pass http://192.168.1.223:8081;
proxy_redirect off;
index /;
}
location /images/ {
alias /apps/filebase/dev/images/;
}
location /resources/ {
alias /apps/filebase/dev/resources/;
}
}
server {
listen 82;
server_name online.zzc;
location /SCM/ {
proxy_pass http://online.zzc;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
index /;
}
location /images/ {
alias /apps/filebase/test_online/images/;
}
location /resources/ {
alias /apps/filebase/test_online/resources/;
}
}
nginx優化
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 image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";
3、解釋一下
第1行:開啟Gzip
第2行:不壓縮臨界值,大于1K的才壓縮,一般不用改
第3行:buffer,
第4行:用了反向代理的話 默認是HTTP/1.1
第5行:壓縮級別,1-10,數字越大壓縮的越好,時間也越長,看心情隨便改吧
第6行:進行壓縮的文件類型,缺啥補啥就行了,JavaScript有兩種寫法,最好都寫上吧,總有人抱怨js文件沒有壓縮,其實多寫一種格式就行了
第7行:跟Squid等緩存服務有關,on的話會在Header里增加"Vary: Accept-Encoding",我不需要這玩意,自己對照情況看著辦吧
第8行:IE6對Gzip不怎么友好,不給它Gzip了
參考文檔:
2.http://www.centoscn.com/image-text/install/2014/0812/3480.html
3.http://www.centoscn.com/image-text/install/2014/0812/3480.html
4.http://www.lxway.com/44841214.htm
5.http://my.oschina.net/flys/blog/200497