nginx安装,配置、虚拟主机、轮询、域名重定向

来源:互联网 发布:ae 编程 编辑:程序博客网 时间:2024/06/02 04:37

一、Nginx 下载
在http://nginx.org官网上下载安装包。nginx-1.12.0.tar.gz
二、.Nginx 安装
1).解压及简单配置
[root@server1 ~]# yum install gcc -y ##安装gcc
[root@server1 ~]# tar zxf nginx-1.12.0.tar.gz ##解压nginx-1.12.0.tar.gz
[root@server1 ~]# ls
nginx-1.12.0 nginx-1.12.0.tar.gz
[root@server1 ~]# cd nginx-1.12.0
这里写图片描述
[root@server1 ~]# vim auto/cc/gcc
178 # debug
179 #CFLAGS=”$CFLAGS -g” ##禁止debug调试
这里写图片描述
这里写图片描述
[root@server1 nginx-1.12.0]# vim src/core/nginx.h
14 #define NGINX_VER “nginx/” ##禁止出现nginx的版本号
这里写图片描述
2)软件配置
[root@server1 nginx-1.12.0]# ./configure –prefix=/usr/local/nginx –with-http_ssl_module –with-file-aio –with-threads –with-http_stub_status_module
如果出现以下错误:
这里写图片描述

[root@server1 nginx-1.12.0]# yum install pcre-devel.x86_64 -y
重新配置:
[root@server1 nginx-1.12.0]# ./configure –prefix=/usr/local/nginx –with-http_ssl_module –with-file-aio –with-threads –with-http_stub_status_module
如果出现以下错误:
这里写图片描述

[root@server1 nginx-1.12.0# yum install openssl-devel.x86_64 -y
重新配置:
[root@server1 nginx-1.12.0]# ./configure –prefix=/usr/local/nginx –with-http_ssl_module –with-file-aio –with-threads –with-http_stub_status_module
这里写图片描述
3.编译、安装
[root@server1 nginx-1.12.0]# make
这里写图片描述
[root@server1 nginx-1.12.0]# make install
这里写图片描述
4、作软链接将nginx的启动命令作为系统命令。
[root@server1 sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@server1 conf]# nginx -t ##检测
[root@server1 conf]# nginx -s reload ##重新加载
这里写图片描述
三、配置
1.创建管理niginx的用户
[root@server1 conf]# useradd -M -d /usr/local/nginx/ -s /sbin/nologin -u 800 nginx
[root@server1 conf]# vim nginx.conf ##修改配置文件
user nginx nginx; ##修改nginx的用户和组
worker_processes 2; ##两块cpu
worker_cpu_affinity 01 10; ##绑定cpu
这里写图片描述
[root@server1 conf]# nginx -t ##检测
[root@server1 conf]# nginx -s reload ##重新加载
2.nginx访问加密
[root@server1 conf]# vim nginx.conf

# HTTPS server    #    server {        listen       443 ssl;        server_name  localhost;        ssl_certificate      cert.pem;        ssl_certificate_key  cert.pem;        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;        }    }}

[root@server1 certs]# pwd
/etc/pki/tls/certs
[root@server1 certs]# make cert.pem ##生成自定义证书
这里写图片描述
[root@server1 conf]# nginx -t
[root@server1 conf]# nginx -s reload
测试:https://172.25.6.1
这里写图片描述

四、虚拟主机
虚拟主机允许一个http服务器同时为多个额昂战提供服务
[root@server1 conf]# vim nginx.conf

server {        listen 80;        server_name www.westos.org;        location / {                root /www;                index index.html;                }}server {        listen 80;        server_name bbs.westos.org;        location / {                root /bbs;                index index.html;        }}

[root@server1 conf]# nginx -t
[root@server1 conf]# nginx -s reload

[root@server1 conf]# mkdir /www ##建立默认发布目录
[root@server1 conf]# cd /www
[root@server1 www]# vim index.html ##建立www.westos.org的默认发布文件

www.westos.server1

[root@server1 www]# mkdir /bbs ##建立默认发布目录
[root@server1 www]# cd /bbs
[root@server1 bbs]# vim index.html ##建立bbs.westos.org的解析文件

bbs.westos.server1

在测试主机里添加域名解析:
[root@foundation6 ~]# vim /etc/hosts
172.25.6.1 server1 www.westos.org bbs.westos.org
这里写图片描述
测试:
在浏览器中输入:www.westos.org bbs.westos.org
这里写图片描述
这里写图片描述

五、轮询
参数说明:
weight:权重。默认为1,weight越大,负载的权重越大。
backup:其他所有的服务器down之后,将会去请求backup服务器。
ip_hash:每个请求按照访问ip的hash结果分配,这样每个访客固定访问 一个后端服务器。
[root@server1 conf]# vim nginx.conf

http {        upstream westos {        server 172.25.6.2:80;  ##服务器1        server 172.25.6.3:80;  ##服务器2        }server {        listen 80;        server_name www.westos.org;   ##域名        location / {                proxy_pass http://westos;                }}

server2:服务器1
[root@server2 ~]# yum install httpd -y ##安装http服务
[root@server2 ~]# /etc/init.d/httpd start ##开启http服务
[root@server2 ~]# cd /var/www/html/
[root@server2 html]# vim index.html ##默认发布目录

www.westos.org-server2

server3:服务器2
[root@server3 ~]# yum install httpd -y ##安装http服务
[root@server3 ~]# /etc/init.d/httpd start ##开启http服务
[root@server3 ~]# cd /var/www/html/
[root@server3 html]# vim index.html ##默认发布目录

www.westos.org-server3

测试:
[root@foundation6 ~]# for i in {1..6}; do curl www.westos.org; done
这里写图片描述
ip_hash
[root@server1 conf]# vim nginx.conf
这里写图片描述
测试:
[root@foundation6 ~]# for i in {1..6}; do curl www.westos.org; done
这里写图片描述
weight
[root@server1 conf]# vim nginx.conf
这里写图片描述
测试:
[root@foundation6 ~]# for i in {1..6}; do curl www.westos.org; done
这里写图片描述
backup
[root@server1 conf]# vim nginx.conf
这里写图片描述
关闭server2:服务器1的http服务
关闭server3:服务器2的http服务
测试:
cur www.westos.org
这里写图片描述
在浏览器里输入www.westos.org,输出的是本机http默认文件
这里写图片描述

六、域名重定向
[root@server1 conf]# vim nginx.conf

server {        listen 80;        server_name westos.org;        rewrite ^(.*) http://bbs.westos.org$1 permanent;   ##bbs.westos.org的域名重定向为westos.org}

在测试主机里添加westos.org的域名解析
[root@foundation6 ~]# vim /etc/hosts
172.25.6.1 server1 www.westos.org bbs.westos.org westos.org
测试:
[root@foundation6 ~]# curl -I westos.org
这里写图片描述
2.加密上传重定向域名
[root@server1 conf]# vim nginx.conf

# HTTPS server    #    server {        listen       443 ssl;        server_name  bbs.westos.org;        ssl_certificate      cert.pem;        ssl_certificate_key  cert.pem;server {        listen 80;        server_name westos.org;        rewrite ^(.*) https://bbs.westos.org$1 permanent;}

[root@server1 conf]# nginx -t
[root@server1 conf]# nginx -s reload
测试:
[root@foundation6 ~]# curl -I westos.org
这里写图片描述

原创粉丝点击