CentOS-6.8 安装 Nginx
Nginx 环境准备:
安装Nginx需要完成以下依赖的安装
1.gcc 安装:
yum install gcc-c++
2.PCRE pcre-devel 安装:yum install -y pcre pcre-devel
3.zlib 安装: yum install -y zlib zlib-devel
4.OpenSSL 安装:yum install -y openssl openssl-devel
安装Nginx
1. 下载Nginx
2. 解压:
tar -zxvf nginx-1.10.1.tar.gz
3. 配置:
cd nginx-1.10.1 进入nginx解压目录
./configure
4. 编译安装:
make
make install
5. 查找安装路径:
whereis nginx
启动、停止Nginx:
1. 查找nginx安装路径:whereis nginx
2. 进入nginx安装路径:cd /usr/local/nginx/sbin
3. 启动:
1. ./nginx
2. /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
两种方式都可以
查看80端口占用情况netstat -tunlp|grep 80
4. 停止:
从容停止:kill -QUIT 19795(注意:19795是Nginx的进程号)
快速停止:kill -TERM 46968(注意:46968是Nginx的进程号)
kill -INT 46975(注意:46975是Nginx的进程号)
强制停止:pkill -9 nginx
./nginx -s stop:
此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
./nginx -s quit:
此方式停止步骤是待nginx进程处理任务完毕进行停止。
先停止再启动:./nginx -s quit; ./nginx
5. 重启:
1. 验证nginx的配置文件是否正确:
1. cd /usr/local/nginx/sbin 进入nginx的安装目录,接着执行 ./nginx -t
如果正确,会有如下的信息:
[root@localhost 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
2. /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
如果正确会有如下信息:
[root@localhost sbin]# /usr/local/nginx/sbin/nginx
-t -c /usr/local/nginx/conf/nginx.conf
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
2. 如果配置文件正确,则可以重启:
1. ./nginx -s reload (需提前进入nginx可执行文件目录:cd /usr/local/nginx/sbin)
2. kill -HUP 46991(46991是nginx的进程号)
6. 重新加载配置文件:
当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,
使用
-s reload
不用先停止 ngin x再启动 nginx 即可将配置信息在 nginx 中生效
例如:
./nginx -s reload
设置Nginx开机自启动:
即在
rc.local
增加启动代码就可以了
查看nginx进程:
1. ps -ef |grep nginx
2.
ps aux|grep nginx
两种方式都可以
二 。
负载均衡配置
我就简单的在一个服务器上启了两个tomcat 然后做了一下测试 可以轮询访问两个服务
后续我会完善该配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream 116.62.70.146{
server 127.0.0.1:9080;
}
server {
listen 80;
server_name nokkin;
location / {
proxy_pass http://116.62.70.146;
}
#charset koi8-r;
#access_log logs/host.access.log main;
#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;
# }
#}
}
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream 116.62.70.146{
server 127.0.0.1:9080;
}
server {
listen 80;
server_name nokkin;
location / {
proxy_pass http://116.62.70.146;
}
#charset koi8-r;
#access_log logs/host.access.log main;
#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;
# }
#}
}
nginx server 配置参数详解
server {
keepalive_requests 120; #单连接请求上限次数。
listen 4545; #监听端口
server_name 127.0.0.1; #监听地址
location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。^开头的意思
#root path; #根目录
#index vv.txt; #设置默认页
proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
deny 127.0.0.1; #拒绝的ip
allow 172.18.5.54; #允许的ip
}
}