CentOS7通过YUM安装Nginx
1.将nginx放到yum repro库中
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2.查看nginx信息
yum info nginx
3.使用yum安装ngnix
yum -y install nginx
4.启动服务
systemctl start nginx
重启:systemctl restart nginx
停止:systemctl stop nginx
查看状态:systemctl status nginx
5.设置开机启动
systemctl enable nginx
详细配置代理
nginx 安装位置:/etc/nginx
可在 /etc/nginx/conf.d 里面配置各种反向代理的配置文件
重点:配置好文件后必须刷新nginx配置,再重启nginx
由于安装会默认把nginx配置到系统环境,可在任何目录直接使用nginx -t 进行刷新
刷新:nginx -t
重启:nginx -s reload
在重启nginx时有可能会遇到一下问题,那就是我们的nginx还未启动,这是需要我们先运行启动服务命令 第4步
[root@yixyz web]# nginx -s reload
nginx: [error] invalid PID number "" in "/run/nginx.pid"
卸载nginx
# 停止nginx
systemctl stop nginx
# 删除Nginx的自动启动
chkconfig nginx off
# 删除nginx文件
rm -rf /usr/sbin/nginx
rm -rf /etc/nginx
rm -rf /etc/init.d/nginx
# 清除yum源
yum remove nginx
Docker 安装 Nginx
# 查看镜像
docker search nginx
# 拉取镜像
docker pull nginx
# 创建挂载目录
mkdir -p /data/nginx/{conf,conf.d,cert.d,html,logs}
目录详解:
conf:放nginx.conf 主要配置文件
conf.d:放一些其他的子配置文件
html:放静态页面
logs:nginx的日志文件
在conf目录下创建nginx.conf
cd /data/nginx/conf
vim nginx.conf
将下列配置放到 nginx.conf 中
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
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 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
# 统一以POST方式转发请求到服务端
# proxy-method=POST;
# 转发请求头里的信息
# proxy_set_header Host $host;
# 转发真实的IP
# proxy_set_header X-Real-IP $remote_addr;
# 转发请求的IP信息列表
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header X-Forwarded-Proto $scheme;
# proxy_set_header X-Forwarded-Port $server_port;
if (!-e $request_filename){
rewrite ^(.*)$ /usr/share/nginx/html/ last; break;
}
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
server 块:
server address [params]
params参数说明:
1.down表示单前的server暂时不参与负载
2.weight为weight越大,负载的权重就越大。
3.max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误
4.fail_timeout:max_fails次失败后,暂停的时间。
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
创建容器
docker run -d --restart=always --name nginx -p 80:80 -p 443:443 -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/conf.d:/etc/nginx/conf.d -v /data/nginx/cert.d:/etc/nginx/cert.d -v /data/nginx/logs:/var/log/nginx nginx
参数说明:
–name:容器名
-p:映射宿主主机端口
-v:挂载宿主目录/容器目录
-d:后台运行容器
–restart=always: docker启动容器自动启动
容器创建成功后通过 IP:8080 就能访问了。