1.检查nginx服务是否运行,如果正在运行则关闭服务。
#检查服务是否在运行
ps -ef|grep nginx
#停止服务
/usr/local/nginx/sbin/nginx -s stop
2.查找并删除nginx相关文件。
#查找nginx相关文件
whereis nginx
#依次删除find找到的所有目录
find / -name nginx
3.卸载nginx依赖。
yum remove nginx
http://nginx.org/en/download.html
1 将安装包上传到服务器指定的目录下
2 对安装包进行解压
tar -zxvf nginx-1.21.6.tar.gz -C /www/server/nginx/
mv nginx-1.21.6 nginx-source
需要提前安装nginx的依赖项
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
1 执行:./configure
2 执行:make && make install
查找并进入工作目录
whereis nginx
cd .....
问题1:You can use subscription-manager to register…
解决方法:关闭订阅管理器功能
[root@localhost yum.repos.d]# vim /etc/yum/pluginconf.d/subscription-manager.conf
[main]
enabled=0
问题2:Repository epel is listed more than once in the configuration…
解决方法:建一个bak目录将除了CentOS-Base.repo之外的文件都移动到bak目录下备份
cd /etc/yum.repos.d
mkdir bak
问题3:Loading mirror speeds from cached hostfile…
解决方法:修改yum源
cd /etc/yum.repos.d
mv CentOS-Base.repo CentOS-Base.repo.backup
wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
mv CentOS7-Base-163.repo CentOS-Base.repo
yum clean all
添加权限
chmod + x configure
所有指令在sbin目录下执行
查看版本:./nginx -v
检测配置:./nginx -t
检测特定配置:./nginx -t -c /usr/local/nginx/conf/nginx.conf -c表示configuration
启动特定配置:./nginx -c /usr/local/nginx/conf/nginx.conf启动命令:./nginx
-s 向正在运行的nginx主进程发送信号,信号的可用值有stop, quit, reopen, reload
暴力停止:./nginx -s stop 快速停止nginx ;
优雅停止:./nginx -s quit 完整有序的停止nginx,这个命令会等待所有请求结束后再关闭nginx
重载配置:./nginx -s reload 平滑的重启,修改配置后,重新加载配置
重启日志:./nginx -s reopen 重新打开日志文件
默认在Linux上安装的Nginx,配置文件在安装的nginx目录下的conf目录下,名字叫做nginx.conf
nginx.conf主要由三部分组成
# 全局快
------------------------------------------------------------------------------
#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块
events {
worker_connections 1024;
}
# http块
http {
------------------------------------------------------------------------------# 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;
------------------------------------------------------------------------------
# server块
server {
# server全局块
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# location块
location / {
root html;
index index.html index.htm;
}
#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;
#}
}
# 可以配置多个server块
}
root与alias区别:
location ^~ /sta/ {
alias /usr/local/nginx/html/static/;
}
请求:http://test.com/sta/sta1.html
实际访问:/usr/local/nginx/html/static/sta1.html 文件
location ^~ /tea/ {
root /usr/local/nginx/html/;
}
请求:http://test.com/tea/tea1.html
实际访问:/usr/local/nginx/html/tea/tea1.html 文件
1 直接访问路径代理
server {
# 监听端口80 即当访问服务器的端口是80时,进入这个server块处理
listen 80;
# server_name当配置了listen时不起作用
server_name localhost;
# location后面代表访问路径 当是/ 请求时 代理到tomcat的地址
location / {
# 使用 proxy_pass(固定写法)后面跟要代理服务器地址
proxy_pass http://192.168.80.102:8080;
}
}
2 通过地址实现不同代理
server {
# 监听9001端口
listen 9001;
# 进行路径匹配,匹配到edu代理到8081
location ~/edu/ {
proxy_pass http://192.168.80.102:8081;
}
# 进行路径匹配,匹配到vod代理到8082
location ~/vod/ {
proxy_pass http://192.168.80.102:8082;
}
}
1 轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除
2 权重
weight 代表权重默认为 1,权重越高被分配的客户端越多
upstream myserver {
server 192.168.80.102:8081 weight=1 ;
server 192.168.80.102:8082 weight=2 ;
}
server {
listen 80;
location / {
proxy_pass http://myserver;
}
3 hash
每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决session问题
#配置负载均衡的服务器和端口
upstream myserver {
server 192.168.80.102:8081;
server 192.168.80.102:8082;
ip_hash;
}
server {
listen 80;
location / {
proxy_pass http://myserver;
}
}
4 fair
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
#配置负载均衡的服务器和端口
upstream myserver {
server 192.168.80.102:8081;
server 192.168.80.102:8082;
fair;
}
server {
listen 80;
location / {
proxy_pass http://myserver;
}
}