參考鏈接
下載並安裝
#使用以下命令
sudo yum install -y nginx
#sudo表示使用管理員權限運行命令
#yum是centos系統中下載安裝程序的命令
#如果提示中發現yum資源庫中沒用Nginx的話,則使用以下命令進行添加
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
Nginx常用命令
#啟動Nginx,使用默認配置文件啟動,如果Nginx沒有關閉,使用此種方式啟動會出現端口被占用的情況
nginx
#停止nginx
nginx -s stop
#如果上面停止nginx的方式無效 可以強制停止
pkill -9 nginx
#重啟nginx
nginx -s reload
#由於在Linux下寫配置文件,容易丟個符號,導致啟動失敗,所以啟動之前可以檢查一下配置文件的正確性
nginx -t
#檢查指定配置文件
nginx -t -c /etc/nginx/nginx.conf
配置
安裝成功之后,想要使用Nginx必須配置配置文件,默認配置文件的地址(/etc/nginx/nginx.conf)
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
#以上配置均是默認值未曾修改,如果想搞懂上面的是什么意思,自己去慢慢學習吧
#這個配置是負載均衡使用的
#此處的app_nodejs是負載均衡的名字
upstream app_nodejs {
#訪問的實際地址是下面的,可以有多個,多個時就達到了負載均衡的作用,后面其實還有一個參數,但是此處寫不寫無區別。
server 127.0.0.1:8082;
keepalive 64;
}
server {
#監聽的是80端口,不建議換成其他端口,因為換成其他端口后,你訪問時,域名也得加上加上端口,比如端口號改成8080,訪問時則是:onloading.cn:8080
listen 80 default;
#訪問的域名
server_name onloading.cn;
#如果訪問的是ip,則直接返回404,此處只允許通過域名訪問
if ($host ~ "\d+\.\d+\.\d+\.\d") {
return 404;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
#指定使用哪個負載均衡,其他location的值均屬於默認值
proxy_pass http://app_nodejs;
proxy_redirect off;
}
}
}
如果想要進行反向代理設置,需要對http中的server節點進行設置,實現反向代理有兩種方式,均是把下面的節點替換掉上面的默認文件的相關節點即可
第一種、使用負載均衡的方式進行反向代理
#app_nodejs名稱是為了下面server找到對應的負載均衡
upstream app_nodejs {
#訪問的實際地址
server 127.0.0.1:8082;
}
server {
#監聽的是80端口,不建議換成其他端口,因為換成其他端口后,你訪問時,域名也得加上加上端口,比如端口號改成8080,訪問時則是:onloading.cn:8080
listen 80 default;
#訪問的域名
server_name onloading.cn;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
#指定使用哪個負載均衡,其他location的值均屬於默認值,即是上面的upstream的名稱
proxy_pass http://app_nodejs;
proxy_redirect off;
}
}
第二種、不使用負載均衡,直接定義反向代理的地址
#該種方式不需要使用upstream節點
server {
#監聽的是80端口,不建議換成其他端口,因為換成其他端口后,你訪問時,域名也得加上加上端口,比如端口號改成8080,訪問時則是:onloading.cn:8080
listen 80 default;
#訪問的域名
server_name onloading.cn;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
#與上面不同的就是,此處指定的是實際訪問的地址
proxy_pass 127.0.0.1:8082;
proxy_redirect off;
}
}
負載均衡
此處參考鏈接
#此處的upstream表示平均分配給三台機器
upstream app_nodejs {
server 192.168.0.100:8080;
server 192.168.0.101:8080;
server 192.168.0.101:8080;
}
server {
listen 80 default;
#訪問的域名
server_name onloading.cn;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
proxy_pass http://app_nodejs;
proxy_redirect off;
}
}
- weight屬性,默認為1,表示平均分配給每台機器。
upstream tomcats {
server 192.168.0.100:8080 weight=2; # 2/6次
server 192.168.0.101:8080 weight=3; # 3/6次
server 192.168.0.102:8080 weight=1; # 1/6次
}
- max_fails屬性,默認為1,表示服務器失敗的最多次數,如果超過該值,表示在fail_timeout時間內請求將不再分配到該服務器上。 如果設置為0,Nginx會將這台Server置為永久無效狀態
- fail_timeout屬性,默認為10秒,表示服務器的失敗無效時長
- backup屬性,備份機,所有服務器失效了之后,啟用該服務器
- down屬性,表示該台服務器無效
upstream tomcats {
#表示100服務器的分配比例是2,失敗最大次數為3,失敗后重新失效時長為15秒
server 192.168.0.100:8080 weight=2 max_fails=3 fail_timeout=15;
#表示101服務器無效
server 192.168.0.101:8080 down;
#表示102服務器為備份服務器
server 192.168.0.102:8080 backup;
}
- max_conns:表示該服務器的最大連接數量,默認為0,表示不限制。注意:1.5.9之后的版本才有這個配置
upstream tomcats {
server 192.168.0.100:8080 max_conns=1000;
}
目前我只用過以上屬性,當然Nginx還有很多其他的屬性,有興趣的可以從網上多找找。