1.安装nginx
[root@localhost ~]# ls /etc/yum.repos.d/
CentOS-Base.repo epel.repo epel-testing.repo
需要有/etc/yum.repo.d/文件夹下的epel.repo和epel-testing.repo文件
因此需要先安装epel
[root@localhost ~]# yum install epel-release
然后就可以安装nginx软件包了
[root@localhost ~]# yum install nginx -y
从安装信息中可以得知安装的nginx版本号:
正在安装 : 1:nginx-1.10.2-2.el7.x86_64
2.配置nginx
浏览器访问服务器是基于url和uri的
统一资源定位符url:http://192.168.86.132:80
通用资源标识符uri:http://192.168.86.132:80/homepage.html
http://说明服务器端是基于http协议给客户端发送文件的,192.168.86.132是服务器ip地址,80是软件端口号(默认80),/homepage.html是要传输的文件地址,/代表nginx指定的根目录
nginx的配置文件是/etc/nginx/目录下的nginx.conf文件
[root@localhost yum.repos.d]# cat /etc/nginx/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; nginx进程数,建议设置成与cpu的数目相同
error_log /var/log/nginx/error.log; 错误日志
pid /run/nginx.pid; nginx的pid文件,开启nginx之后自动生成,记录nginx的pid
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; 针对nginx当前模块的配置文件
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"'; 定义一个叫做main的日志格式
access_log /var/log/nginx/access.log main; 指定访问日志文件 指定访问日志格式为main
sendfile on;
tcp_nopush on; nginx服务是基于tcp协议工作的
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
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 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html; nginx的根目录,
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# 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;
#
# location / { 用户提交的uri请求如果是/开头的则加载loction中的配置
root /var/www/html/; 指定root路径
index index.txt index.htm index.html; 指定访问文件的优先级
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
配置好nginx之后需要重新加载配置文件:
systemctl reload nginx
关闭防火墙:
systemctl stop firewalld
在浏览器中输入http://192.168.86.132:80/a/b/c.txt就可以访问该文件了
3.nginx反向代理
反向代理有四种模式:1.轮询2.最小访问数3.ip访问4.加权访问
首先需要定义反向代理组和确定反向代理模式:
upstream pythonweb{
ip_hash;
# least_conn;
server 192.168.86.133;
server 192.168.86.134;
server 192.168.86.135;
#server 192.168.86.133 weight=3;
#server 192.168.86.134 weight=3;
#server 192.168.86.135 weight=1;
}
修改主机nginx配置文件下的location中的root路径为指向反向代理组:
location / {
proxy_pass http://pythonweb;
index index.txt index.htm index.html;
}
保存退出后重新加载配置文件即可,这时访问主机就会自动转到反向代理组并读取不同代理机中对应的文件。