lnmp之nginx

来源:互联网 发布:软考程序员有必要考吗 编辑:程序博客网 时间:2024/06/05 21:00


##################################nginx##################################
一,Nginx安装

一般安装,先解压;

然后进行预编译,一般预编译会带上一些参数,达到我们想要的效果。

首先,安装nginx的依赖,(如果不安装,安装过程中,会有提醒。)

yum install openssl-devel -y
yum install pcre-devel -y

解压源码包:
tar zxf nginx-1.12.0.tar.gz

修改nginx的配置文件gcc:
vim
 
/nginx-1.12.0/auto/cc/gcc
# debug
#CFLAGS="$CFLAGS -g"-->(
注释掉这行,去掉 debug 模式编译,编译以后程序只有几百 k)

进入源码包目录,进行配置,预编译:
cd nginx-1.12.0

修改该文件,使nginx的版本不显示,保其安全性。
vim
 
src/core/nginx.h
14 #define NGINX_VER         "nginx"--->
去掉后面的 “ NGINX_VERSION”,不显示nginx的版本(为了安全)

建立nginx用户:
useradd -M -d /usr/local/lnmp/nginx -s /sbin/nologin -u 800 nginx

进行预编译:
./configure --prefix=/usr/local/lnmp/nginx --user=nginx --group=nginx--with-threads --with-file-aio
  
--with-http_ssl_module --with-http_stub_status_module

编译,安装:
make && make install

建立软连接,便于启动nginx:
ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/sbin/

**
测试:
在此主机上:
curl -I localhost

在其他主机上:
curl -I 172.25.26.1
如图:
HTTP/1.1 200 OK
Server: nginx
  -->
可以看到不显示版本信息
Date: Thu, 20 Jul 2017 08:55:33 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 20 Jul 2017 08:27:18 GMT
Connection: keep-alive
ETag: "59706966-264"
Accept-Ranges: bytes



############
想重新安装配置######################
rm -fr /usr/local/lnmp/nginx/
重新编译时,需要清除旧的对象文件和缓存信息
cd /root/nginx-1.12.0
make clean

rm -fr /root/nginx-1.12.0

##
重新解压:tar zxf nginx-1.12.0.tar.gz

**
再重复以上即可。


##################################
绑定cpu(2个以上)####################

virt-manager
CPU换成2

####
更改配置
cd /usr/local/lnmp/nginx/conf
vim nginx.conf
1)#####cpu
绑定
 3 worker_processes 2;--->2
CPU工作
 5 worker_cpu_affinity 01 10;--->2
个进程

ps ax
    ##
查看进程
如图:
1061 ?       S     0:00 nginx: worker process
1062 ?       S     
0:00 nginx: worker process


2
####max_file的限制

14 events {
15
     worker_connections 
65535;
16}

vim /etc/security/limits.conf
nginx
           -      nofile         
65535


***
测试:
usermod -s /bin/bash nginx

su - nginx
ulimit -a

usermod -s /sbin/nologin nginx


3)
服务

server {
       
listen 80;
       
server_name www.westos.org;

       
location {
               
root /web1;
               
index index.html;
}
}

nginx -t
nginx -s reload

mkdir /web1
cd /web1/
vim index.html
<h1>server1-web1.www.westos.org</h1>


***
测试:
在客户端,加入本地解析
172.25.90.1 www.westos.org
在浏览器,输入:www.westos.org
可看到:
server1-web1.www.westos.org


4)
证书加密
cd /etc/tls/private/
openssl genrsa 2048 > localhost.key

cd /etc/tls/certs/
make cert.pem
mv cert.pem /usr/local/lnmp/nginx/conf/



cd /usr/local/lnmp/nginx/conf/
vim nginx.conf
 99    
server {
100        listen      
443 ssl;
101        server_name 
localhost;
102
103
         ssl_certificate     
cert.pem;
104        ssl_certificate_key 
cert.pem;-----改成pem
105
106
         ssl_session_cache   
shared:SSL:1m;
107        ssl_session_timeout 
5m;
108
109
         ssl_ciphers 
HIGH:!aNULL:!MD5;
110        ssl_prefer_server_ciphers 
on;
111
112
        
location / {
113            root  
html;
114            index 
index.html index.htm;
115        
}
116    
}

nginx -t
nginx -s reload

在浏览器,访问:https://172.25.90.1/


5)
控制访问

vim nginx.conf
 49        
location /status {
 50                
stub_status on;
 51                
access_log off;
 52                
allow 127.0.0.1;
 53                
deny all;
 
54 }

nginx -t
nginx -s reload
访问:172.25.27.1/status
curl localhost/status


6)
网页重写

vim nginx.conf
106
     
server {
107        listen      
443 ssl;
108        server_name 
www.westos.org;
109
110
    
111        ssl_certificate     
cert.pem;
112        ssl_certificate_key 
cert.pem;
113

125
        
server{
126                listen         
80;
127                server_name 
www.westos.org;
128                
rewrite ^(.*)$https://www.westos.org$1 permanent;  ##重写网页
129        
}


nginx -t
nginx -s reload

访问:https://www.westos.org/

vim index.html
<h1>admin page</h1>

访问:https://www.westos.org/admin

vim /etc/hosts
172.25.27.1
    
server1www.westos.org
curl www.westos.org -I
   
HTTP/1.1 301 Moved Permanently

vim nginx.conf
128
                
rewrite ^(.*)$https://www.westos.org$1 redirect;

测试:
curl -I www.westos.org
HTTP/1.1 302 Moved Temporarily

7)
反向代理配置

vim nginx.conf
18 http {
19
        
upstream westos {
20        
server 172.25.90.2:80;
21        
server 172.25.90.3:8080;
     
server 127.0.0.1:8000 backup;
22 }

133
              
# rewrite ^(.*)$https://www.westos.org$1 redirect;
134                
location / {
135                        proxy_passhttp://westos; ##--->
反向代理模块(自定义,也叫,反向代理负载均衡)--域名:westos
136                
}

nginx -t
nginx -s reload

server1:
vim /etc/httpd/conf/httpd.conf
136 Listen 8000

/etc/init.d/httpd start

server2:
/etc/init.d/httpd start
server3:
vim /etc/httpd/conf/httpd.conf
 
136 Listen 8080
/etc/init.d/httpd start

server1测试:
[root@server1 conf]# for i in {1..10}; do curl www.westos.org;done
<h1>server2-westos.org</h1>
<h1>server3-westos.org</h1>
<h1>server2-westos.org</h1>
<h1>server3-westos.org</h1>
<h1>server2-westos.org</h1>
<h1>server3-westos.org</h1>
<h1>server2-westos.org</h1>
<h1>server3-westos.org</h1>
<h1>server2-westos.org</h1>
<h1>server3-westos.org</h1>


****
外加:权重
18 http {
 19        
upstream westos {
 20        
server 172.25.90.2:80 weight=2;###权重
 21        
server 172.25.90.3:8080;
 22        
server 127.0.0.1:8000 backup;

nginx -t
nginx -s reload

访问:
[root@server1 conf]# for i in {1..10}; do curl www.westos.org;done
<h1>server2-westos.org</h1>
<h1>server3-westos.org</h1>
<h1>server2-westos.org</h1>
<h1>server2-westos.org</h1>
<h1>server3-westos.org</h1>
<h1>server2-westos.org</h1>
<h1>server2-westos.org</h1>





8
)健康检查:

vim nginx.conf
http {
       
upstream westos {
       ip_hash;---->
确定一个ip,一直访问此后台服务端的
       server 172.25.90.2:80weight=2;---->
确定一个ip,一直访问此后台服务端的
       server 172.25.90.3:8080;
       
#server 127.0.0.1:8000backup;

nginx -t
nginx -s reload

测试:
[root@server1 conf]# for i in {1..10}; do curl www.westos.org;done
<h1>server3-westos.org</h1>
<h1>server3-westos.org</h1>
<h1>server3-westos.org</h1>
<h1>server3-westos.org</h1>









原创粉丝点击