nginx安装,配置,https

2017-06-05  本文已影响0人  adtk
# 依赖环境
yum install gcc-c++
yum -y install pcre-devel openssl openssl-devel

wget http://nginx.org/download/nginx-1.5.9.tar.gz  (下载包)
#18年3月7日:http://nginx.org/download/nginx-1.13.9.tar.gz

tar -zxvf nginx-1.5.9.tar.gz (解压)

cd  nginx-1.5.9 (进入解压后的目录)

./configure --prefix=/usr/local/nginx  --with-http_stub_status_module --with-http_ssl_module --with-stream --with-http_v2_module
//注释:(prefix指定安装目录,with-http_ssl_module安装ssl模块(https))

make && make install (编译安装)

最终安装在/usr/local/nginx/目录下

配置ssl:实现https+http并存

//修改/usr/local/nginx/conf/nginx.conf
 server { 
        #指定虚拟主机的服务端口 
        listen 80; 
        listen 443 ssl; 
        server_name localhost; #指定IP地址或者域名 
        #ssl on;  #这行一定要注释掉 
        ssl_certificate  server.crt;  #两个证书自己申请
        ssl_certificate_key  server.key; 
}

实现多站点配置(指向不同端口,我用于node),并实现http跳转https

注:还可以分文件实现,百度下。复制时注意换行符,容易出错,被坑很久

     server {
        listen       80;
        server_name  y.adtk.cn;
        location / {
            proxy_pass   http://127.0.0.1:8090;
        }
    }
    server {
        listen       80;
        server_name         adtk.cn;   #localhost
        return 301 https://$server_name$request_uri;
    }
    server {
        listen 443 ssl; 
        server_name  adtk.cn; 

        ssl_certificate 1_adtk.cn_bundle.crt; 
        ssl_certificate_key 2_adtk.cn.key; 
        location / {
            proxy_pass   http://127.0.0.1:8080;
        }
    }

在Nginx扩展设置(location段)添加如下代码,实现代理端口:

proxy_pass http://127.0.0.1:8080;

启动:

/usr/local/nginx/sbin/nginx
若在全局:nginx

查看配置信息是否正确

/usr/local/nginx/sbin/nginx -t
若在全局:nginx -t

配置php

        location ~ \.php$ {
            root           /web/php;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            # fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

重启

进入nginx可执行目录sbin下(/usr/local/nginx/sbin/),输入命令
./nginx -s reload
若在全局:nginx -s reload

强制停止Nginx:

pkill -9 nginx

http://nginx.org/

上一篇下一篇

猜你喜欢

热点阅读