nginx安装

来源:互联网 发布:淘宝怎么换付款方式 编辑:程序博客网 时间:2024/04/29 01:50
编译安装Nginx
所需软件(1)pcre-7.7.tar.gz  (2)nginx-0.7.64.tar.gz
安装nginx需要pcre的支持===
软件下载:
http://sysoev.ru/nginx/download.html
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
http://www.zlib.net/

1、安装pcre
tar -zxvf pcre-7.7.tar.gz
cd pcre-7.7
./configure && make && make install

如:安装报错一般都是因为没有gcc库导致:要用yum安装
#安置gcc编译器可以编译C步骤,但是不支撑c++
yum install gcc
#安置g++(安置后可以编译C++步骤)
yum install gcc-c++
#安置gdb(调试东西)
yum install gdb

2、nginx的安装
tar -xzvf nginx-0.7.64.tar.gz

cd nginx-0.7.64

默认路径方式一:

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module && make && make install
方式二:
./configure --prefix=/usr/nginx --with-http_stub_status_module --with-pcre=/usr/nginx/pcre-7.7/ --without-select_module --without-poll_module && make && make install


要查看nginx监控状态,请修改nginx.conf如下配置:

server{  

         location /nginx-status {  

             allow --------  

             allow --------//允许的ip  

             deny all;//  

             stub_status on;  

             access_log  off;  

        }  


访问http:ip:port/nginx-status即可,下面是结果 

Active connections: 5   

server accepts handled requests  

 5970806143 5970806143 7560482010   

Reading: 0 Writing: 5 Waiting: 0   

Active connections: 对后端发起的活动连接数.

Server accepts handled requests: Nginx总共处理了38810620个连接,成功创建38810620次握手(证明中间没有失败的),总共处理了298655730个请求.

Reading: Nginx 读取到客户端的Header信息数.

Writing: Nginx 返回给客户端的Header信息数.

Waiting: 开启keep-alive的情况下,这个值等于 active – (reading + writing),意思就是Nginx已经处理完成,正在等候下一次请求指令的驻留连接.

所以,在访问效率高,请求很快被处理完毕的情况下,Waiting数比较多是正常的.如果reading +writing数较多,则说明并发访问量非常大,正在处理过程中.

====================================================
启动
cd /usr/local/nginx/sbin/./nginx
停nginx
cd /usr/local/nginx/sbin/
kill all nginx
如果有几套nginx那么要
cat ../nginx.pid

kill PID

pkill nginx


平滑启动===

kill -HUP `cat nginx.pid`


启动报如下错误,需要修改nginx.conf配置端口80为其他端口。

nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)


===================================================

suse nginx的https配置:

安装确保已经安装了https模块,如下:
./configure --prefix=/usr/local/sns/nginx/ --with-pcre=/usr/local/sns/pcre-7.7 --with-openssl=/usr/local/sns/openssl-1.0.1c --with-http_stub_status_module --with-http_ssl_module --group=sns --user=sns

#cd /usr/local/nginx/conf
#mkdir ssl
#cd ssl
生成一个私有key
openssl genrsa -des3 -out sns.com.key 1024

openssl req -new -key sns.com.key -out sns.com.csr

 cp sns.com.key sns.com.key.bak
 
openssl rsa -in sns.com.key.bak -out sns.com.key


如果没有证书,自己生产一个测试证书:

openssl x509 -req -days 365 -in sns.com.csr -signkey sns.com.key -out sns.com.crt

nginx的配置如下:

server {
                server_name test.com;
                listen  443;
                 ### SSL log files ###  
                access_log      logs/ssl-access.log;  
                error_log       logs/ssl-error.log;  
                ssl on;
                ssl_certificate ssl/sns.com.crt;
                ssl_certificate_key ssl/sns.com.key;
                ### Add SSL specific settings here ###  
                keepalive_timeout    60;  
                 ###  Limiting Ciphers ########################  
                # Uncomment as per your setup  
                #ssl_ciphers HIGH:!ADH;  
                #ssl_perfer_server_ciphers on;  
                #ssl_protocols SSLv3;  
                 ##############################################  
                ### We want full access to SSL via huawei ###  
                location / {  
                        proxy_pass  http://test;     
                        ### Set headers ####  
                        proxy_set_header Host $host;  
                         proxy_set_header X-Real-IP $remote_addr;  
                         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
   
                         ### Most PHP, Python, Rails, Java App can use this header ###  
                        proxy_set_header X-Forwarded-Proto https;  
   
                 ### By default we don't want to redirect it ####  
                        proxy_redirect     off;  
                 }  
        }


注:证书链的生成是以点金证书的路径查看得知,如下:


根据这个顺序来生成证书链即可。

操作命令大致如下:

 cat .messages.megafon.ru.crt COMODOSSLCA.crt AddTrustExternalCARoot.crt > sns.crt


其中第三方提供的证书建上图