Nginx安裝
1、下載下傳安裝包安裝:
[root@centos7 package]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
tar zxvf nginx-1.12.1.tar.gz
cd nginx-1.12.1/
./configure --prefix=/usr/local/nginx
make && make install
echo $?
2、建立啟動腳本
[root@centos7 nginx-1.12.1]# cat /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop()
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
reload()
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
restart()
stop
start
configtest()
$NGINX_SBIN -c $NGINX_CONF -t
return 0
case "$1" in
start)
start
;;
stop)
stop
reload)
reload
restart)
restart
configtest)
configtest
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
3、檢查文法
[root@centos7 nginx-1.12.1]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
4、chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
5、建立配置檔案nginx.conf
[root@centos7 nginx-1.12.1]# cd /usr/local/nginx/conf/
You have new mail in /var/spool/mail/root
[root@centos7 conf]# mv nginx.conf nginx.conf.bak
[root@centos7 conf]# vi nginx.conf
user nobody nobody;
#定義啟動Nginx的使用者
worker_processes 2;
#定義子程序數目
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
#指定Nginx最多可打開的檔案數目
events
use epoll;
worker_connections 6000;
#程序最大連接配接數
http
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
server
#虛拟主機
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$
#配置PHP解析
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
6、啟動
[root@centos7 conf]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ OK ]
驗證nginx:
1、
[root@centos7 conf]# vim /usr/local/nginx/html/1.php
<?php
echo "welcom to nginx1.12 text";
?>
[root@centos7 conf]# curl localhost/1.php
welcom to nginx1.12 text
預設虛拟主機
1、在配置檔案中新增“include vhost/*.conf;”
[root@centos7 conf]# cd /usr/local/nginx/conf
[root@centos7 conf]# vim /usr/local/nginx/conf/nginx.conf
include vhost/*.conf;
[root@centos7 conf]# mkdir vhost
[root@centos7 conf]# cd vhost/
[root@centos7 vhost]# vim aaa.com.conf
2、
3、
server
listen 80 default_server;
#有'default_server'标記的就是預設虛拟主機
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
4、
[root@centos7 vhost]# mkdir -p /data/wwwroot/default
[root@centos7 vhost]# cd /data/wwwroot/default
[root@centos7 default]# vim index.html
This is the default directory.
5、加載
[root@centos7 default]# /usr/local/nginx/sbin/nginx -s reload
6、驗證
[root@centos7 default]# curl 127.0.0.1:80
This is the default directory
Nginx使用者認證
[root@centos7 vhost]# vim test.com.conf
listen 80;
server_name test.com;
root /data/wwwroot/test.com;
location /
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
#指定使用者的密碼檔案
2、建立密碼檔案
需要用到Apache的/usr/local/apache/bin/htpasswd指令;若是沒有,需要安裝:
yum install -y httpd
3、建立密碼檔案
[root@centos7 default]# htpasswd -c /usr/local/nginx/conf/htpasswd shenjie
New password:
Re-type new password:
Adding password for user shenjie
第一次建立就用-c;第二次就需要用-m,不然還是用-c會覆寫之前建立的
4、建立web站點目錄
[root@centos7 default]# mkdir /data/wwwroot/test.com
[root@centos7 default]# echo "This is test.com" >/data/wwwroot/test.com/index.html
驗證:
[root@centos7 default]# curl -x127.0.0.1:80 test.com -ushenjie:123456
This is test.com
配置.php結尾的檔案
1、[root@centos7 test.com]# vim /usr/local/nginx/conf/vhost/test.com.conf
location ~ \.php$
#比對以php結尾的檔案
fastcgi_pass unix:/tmp/php-fcgi.sock; #也可以用127.0.0.1:9000
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
}
[root@centos7 test.com]# curl -x127.0.0.1:80 test.com/index.php
test.com php.index
Nginx域名重定向
server_name test.com test2.com test3.com;
#使用多個域名,當主機不是test.com時就跳轉到test.com域名上,permanent301永久跳轉
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
#使用rewrite子產品
[root@centos7 test.com]# curl -x127.0.0.1:80 test3.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Fri, 10 Nov 2017 08:51:28 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/
擴充:Nginx配置檔案詳解
#定義Nginx運作的使用者和使用者組
user www www;
#nginx程序數,建議設定為等于CPU總核心數。
worker_processes 8;
#全局錯誤日志定義類型,[ debug | info | notice | warn | error | crit ]
error_log /var/log/nginx/error.log info;
#程序檔案
pid /var/run/nginx.pid;
#一個nginx程序打開的最多檔案描述符數目,理論值應該是最多打開檔案數(系統的值ulimit -n)與nginx程序數相除,但是nginx配置設定請求并不均勻,是以建議與ulimit -n的值保持一緻。
worker_rlimit_nofile 65535;
#工作模式與連接配接數上限
#參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本核心中的高性能網絡I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
use epoll;
#單個程序最大連接配接數(最大連接配接數=連接配接數*程序數)
worker_connections 65535;
#設定http伺服器
include mime.types; #檔案擴充名與檔案類型映射表
default_type application/octet-stream; #預設檔案類型
#charset utf-8; #預設編碼
server_names_hash_bucket_size 128; #伺服器名字的hash表大小
client_header_buffer_size 32k; #客戶請求頭緩沖大小吧
large_client_header_buffers 4 64k; #設定請求緩
client_max_body_size 8m; #上傳檔案大小限制
sendfile on; #開啟高效檔案傳輸模式,sendfile指令指定nginx是否調用sendfile函數來輸出檔案,對于普通應用設為 on,如果用來進行下載下傳等應用磁盤IO重負載應用,可設定為off,以平衡磁盤與網絡I/O處理速度,降低系統的負載。注意:如果圖檔顯示不正常把這個改成off。
autoindex on; #開啟目錄清單通路,合适下載下傳伺服器,預設關閉。
tcp_nopush on; #防止網絡阻塞
tcp_nodelay on; #防止網絡阻塞
keepalive_timeout 120; #長連接配接逾時時間,機關是秒
#FastCGI相關參數是為了改善網站的性能:減少資源占用,提高通路速度。下面參數看字面意思都能了解。
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#gzip子產品設定
gzip on; #開啟gzip壓縮輸出
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;
#壓縮類型,預設就已經包含text/html,是以下面就不用再寫了,寫上去也不會有問題,但是會有一個warn。
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m; #開啟限制IP連接配接數的時候需要使用
upstream blog.ha97.com {
#upstream的負載均衡,weight是權重,可以根據機器配置定義權重。weigth參數表示權值,權值越高被配置設定到的幾率越大。
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
#虛拟主機的配置
#監聽端口
listen 80;
#域名可以有多個,用空格隔開
server_name www.ha97.com ha97.com;
index index.html index.htm index.php;
root /data/www/ha97;
location ~ .*\.(php|php5)?$
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
#圖檔緩存時間設定
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
expires 10d;
#JS和CSS緩存時間設定
location ~ .*\.(js|css)?$
expires 1h;
#日志格式設定
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#定義本虛拟主機的通路日志
access_log /var/log/nginx/ha97access.log access;
#對 "/" 啟用反向代理
location / {
proxy_pass http://127.0.0.1:88;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
#後端的Web伺服器可以通過X-Forwarded-For擷取使用者真實IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#以下是一些反向代理的配置,可選。
proxy_set_header Host $host;
client_max_body_size 10m; #允許用戶端請求的最大單檔案位元組數
client_body_buffer_size 128k; #緩沖區代理緩沖使用者端請求的最大位元組數,
proxy_connect_timeout 90; #nginx跟後端伺服器連接配接逾時時間(代理連接配接逾時)
proxy_send_timeout 90; #後端伺服器資料回傳時間(代理發送逾時)
proxy_read_timeout 90; #連接配接成功後,後端伺服器響應時間(代理接收逾時)
proxy_buffer_size 4k; #設定代理伺服器(nginx)儲存使用者頭資訊的緩沖區大小
proxy_buffers 4 32k; #proxy_buffers緩沖區,網頁平均在32k以下的設定
proxy_busy_buffers_size 64k; #高負荷下緩沖大小(proxy_buffers*2)
proxy_temp_file_write_size 64k;
#設定緩存檔案夾大小,大于這個值,将從upstream伺服器傳
#設定檢視Nginx狀态的位址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
#htpasswd檔案的内容可以用apache提供的htpasswd工具來産生。
#本地動靜分離反向代理配置
#所有jsp的頁面均交由tomcat或resin處理
location ~ .(jsp|jspx|do)?$ {
proxy_pass http://127.0.0.1:8080;
#所有靜态檔案由nginx直接讀取不經過tomcat或resin
location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{ expires 15d; }
location ~ .*.(js|css)?$
{ expires 1h; }
本文轉自 jiekegz 51CTO部落格,原文連結:http://blog.51cto.com/jacksoner/1981911