一、安裝
1、下載(此處不再贅述,忽略)
2、解壓到指定目錄
[root@localhost home]# tar zxvf nginx-1.11.2.tar.gz -C /myapp/
[root@localhost home]# cd /myapp/nginx-1.11.2/
[root@localhost nginx-1.11.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
3、使用yum安裝依賴庫 【gcc 、pcre、openssl、zilib】
[root@localhost nginx-1.11.2]# yum -y install gcc
[root@localhost nginx-1.11.2]# yum -y install pcre
[root@localhost nginx-1.11.2]# yum -y install openssl
[root@localhost nginx-1.11.2]# yum -y install zlib
4、編譯并安裝
[root@localhost nginx-1.11.2]# ./configure –prefix=/usr/local/nginx –with-http_stub_status_module
[root@localhost nginx-1.11.2]# make
[root@localhost nginx-1.11.2]# make install
5、說明:
參數(shù) –with-http_stub_status_modul是為了啟用nginx的 NginxStatus 功能,用來監(jiān)控 Nginx 的當(dāng)前狀態(tài)
–prefix=/usr/local/nginx 指定安裝目錄更詳細(xì)的參數(shù)參考./configure –help
安裝成功后 /usr/local/nginx 目錄下有四個(gè)子目錄分別是:conf、html、logs、sbin 。
二、配置Nginx
1、配置
#user nobody;
user root root; #工作進(jìn)程的屬主
worker_processes 1; #工作進(jìn)程數(shù),一般與 CPU核數(shù)等同
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
############################################################################
# 使用的網(wǎng)絡(luò)I/O模型,Linux系統(tǒng)推薦采用epoll模型,F(xiàn)reeeBSD系統(tǒng)推薦采用kqueue模型use epoll;
############################################################################
worker_connections 1024; #每個(gè)工作進(jìn)程允許最大的同時(shí)連接數(shù)
}
http {
include mime.types;
default_type application/octet-stream;
############################################################################
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
# 設(shè)置客戶端能夠上傳的文件大小,注意要與應(yīng)用程序中的文件大小限制兼容。
client_max_body_size 10m;
# sendfile on;
tcp_nopush on;
# keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
# 以下放開注釋 自定義日志記錄格式設(shè)置,main為名字,在access_log命令中引用
log_format main '$remote_addr – $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#指定日志存放路徑,如果想使用默認(rèn)的combined格式記錄日志,可以使用access_log logs/access.log combined; 以下是使用log_format自定義的格式記錄日志的。
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
############################################################################
# 開啟gzip壓縮設(shè)置(只能在http模塊中設(shè)置)
gzip on; #放開注釋
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types application/x-javascript text/css application/xml;
gzip_vary on;
#upstream設(shè)置,設(shè)置代理服務(wù)器(負(fù)載均衡池),默認(rèn)的負(fù)載均衡方式是輪詢,另外一種是ip_hash
upstream tomcat_server{
ip_hash; #每個(gè)請(qǐng)求按訪問ip的hash結(jié)果分配,這樣每個(gè)訪客固定訪問一個(gè)后端服務(wù)器,可以解決session的問題。
server 10.10.10.145:8081 weight=10;
server 10.10.10.146:8082 weight=10;
}
#處理上傳和下載的圖片文件服務(wù)器,設(shè)置代理服務(wù)器(負(fù)載均衡池),默認(rèn)的負(fù)載均衡方式是輪訓(xùn),另外一種是ip_hash
upstream image_server{
server 10.10.10.148:8083 weight=10;
}
############################################################################
server {
listen 8090; #nginx偵聽端口 根據(jù)自己需要修改 我的8080端口已經(jīng)被占用,所以修改為8090
server_name localhost;
############################################################################
charset utf-8;
index index.html index.htm index.jsp index.do;
############################################################################
#charset koi8-r;
############################################################################
#rewrite settings
if (-d $request_filename)
{
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
############################################################################
#access_log logs/host.access.log main;
location / {
#root html;
index index.html index.htm index.jsp index.do;
}
############################################################################
# iamge 服務(wù)器location
location ~*/admin/images/{
# alias /web/www/html/img/;
proxy_pass http://image_server;
proxy_set_header Host $host:8083;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~*/admin/
{
# 如果后端服務(wù)器返回502,504,執(zhí)行超時(shí)等錯(cuò)誤,自動(dòng)將請(qǐng)求轉(zhuǎn)發(fā)到upstream負(fù)載均衡池中的另一臺(tái)服務(wù)器,實(shí)現(xiàn)failover。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://tomcat_server;
proxy_redirect off;
# 變量$host等于客戶端請(qǐng)求頭中的Host值
#proxy_set_header Host $host:8081;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
#后端的web服務(wù)器可以通過X-Forwarded-For獲取真實(shí)的IP地址,$remote_addr客戶端的ip地址
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#image expires settings
# expires 屬于http Header模塊,主要用來Nginx返回給用戶網(wǎng)頁添加附件的header信息,可以在http,server,location中使用
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
#css&js expires settings
# expires 屬于http Header模塊,主要用來Nginx返回給用戶網(wǎng)頁添加附件的header信息,可以在http,server,location中使用
location ~ .*.(js|css|html)$
{
expires 2h;
}
# 放開注釋
error_page 404 /404.html;
############################################################################
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
2、測試
測試配置文件是否正確
[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
如果顯示以下信息,說明配置正確可以啟動(dòng)Nginx服務(wù)
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
3、啟動(dòng)、重啟、停止、重新加載配置文件命令
啟 動(dòng)nginx: /usr/local/nginx/sbin/nginx
重 啟Nginx: /usr/local/nginx/sbin/nginx -s reload
停 止Nginx:/usr/local/webserver/nginx/sbin/nginx -s stop
重新加載配置文件:/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf