CentOS7 - 之Nginx-1.12.2安装
Nginx是一个出色的反向代理
服务器,可以很好的代理服务端,处理客户端的请求。今天我们了解以下Nginx在CentOS7上的安装。
- 注意!
正向代理
是代理的的客户端,是代理服务器帮客户端去访问其它的服务端。
1、首先安装依赖的c语言的编译环境
# 使用root权限进行操作
sudo yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
2、进行解压安装
# 1 解压
tar -zxvf nginx-1.12.2.tar.gz
# 2 进入home目录
cd nginx-1.12.2
# 3 /opt/module/nginx 要安装的目录
./configure --prefix=/opt/module/nginx
# 4 编译
make && make install
# 5
cd /opt/module/nginx
- nginx默认占用80端口,linux默认情况下非root用户不允许使用1024以下的端口
# 让当前用户的某个应用可以使用1024以下的端口
sudo setcap cap_net_bind_service=+eip /opt/module/nginx/sbin/nginx
# 启动 nginx
sbin/nginx
# 验证是否启动
ps -ef | grep nginx
[shufang@shufang101 nginx]$ ps -ef | grep nginx
shufang 18916 1 0 21:43 ? 00:00:00 nginx: master process sbin/nginx
shufang 18917 18916 0 21:43 ? 00:00:00 nginx: worker process
shufang 18973 8282 0 21:43 pts/0 00:00:00 grep --color=auto nginx
- 还可以访问 :shufang101:80
3、Nginx的启停
# 启动(指定配置文件启动)
sbin/nginx -c /opt/module/nginx/conf/nginx.conf
# 停止
sbin/nginx -s stop
# 检查配置文件是否可用
sbin/nginx -c /opt/module/nginx/conf/nginx.conf -t
[shufang@shufang101 nginx]$ sbin/nginx -c /opt/module/nginx/conf/nginx.conf -t
nginx: the configuration file /opt/module/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/module/nginx/conf/nginx.conf test is successful
4 Nginx配置文件
[shufang@shufang101 conf]$ cat nginx.conf
#user nobody; # 启停服务的用户
worker_processes 1; # 服务的Worker的数量
#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; # 每个worker在同一时间能同时处理的请求的数量
}
# http配置
http {
include mime.types; # 可以处理的类型,不能处理Java代码,只能是静态的内容
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;
# 在server外部配置反向代理 ==========================================1
upstream www.logserver.com{
server shufang101:8081 weight=1;
server shufang102:8081 weight=2;
server shufang103:8081 weight=3;
}
#gzip on;
# 用来配置反向代理服务器
server {
listen 80;
server_name localhost;
#charset koi8-r;
#*******************************************************************************
#access_log logs/host.access.log main;
# 当请求 localhost:80 的时候,都会被location拦截,跳转到 home/html/index.html的页面
#location / {
# root html;
# index index.html index.htm;
#}
#******************************************************************************/
# 在server内部配置我们处理日志对方开放的接口/applog ===================================================2
location /applog{
proxy_pass http://www.logserver.com;
}
#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;
# }
#}
}
5 Nginx的主要应用
- 静态网站部署
- 动静分离
- 静态代理
- 负载均衡
- 虚拟主机