搭建Nginx以及配置https

搭建Nginx以及配置https

Nginx是一个高性能的HTTP反向代理服务器,也是一个IMAP/POP3/SMTP服务器。在高连接并发的情况下,Nginx与Apache相比,Nginx占用资源更少,并且配置更加灵活,轻量。

Nginx安装

环境

Ubuntu 16.04 i386

安装Nginx

我们将使用ubuntu默认的方式来安装nginx,尽管安装的版本不一定是最新的,但是使用apt-get的方式安装软件,就一个字,省心。

# apt-get update
# apt-get install nginx
(查看nginx版本)
# nginx -v
nginx version: nginx/1.10.3 (Ubuntu)

安装完成之后的Nginx目录

# 默认的web目录
/usr/share/nginx/html

# nginx.conf
/etc/nginx/nginx.conf

# nginx
/usr/sbin/nginx

# 默认网站的conf
/etc/nginx/sites-enabled/default

## 日志目录
/var/log/nginx

打开http://hostip, 显示以下页面表示安装成功

搭建Nginx以及配置https

如果不能访问,也不要着急,先看看返回什么错误,一般有下面错误:

端口80被占用: Starting nginx: [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)

解决方法:找到占用80端口的进程,干掉该进程,然后重启Nginx

403 error

解决方法:一般是Nginx配置文件里的网站路径不正确,可以用nginx -t 验证一下配置信息

php 7.0

Nginx只是一个web服务器,默认不支持php,如果要打开php编写的服务器,需要通过php-fpm(管理fastcgi 的进程)来处理请求。

nginx和php-fpm可以通过监听9000端口或者socket(默认)来实现。

  1. 安装php-fpm
sudo apt-get install php7.0-fpm
  1. 修改/etc/nginx/sites-enabled/default 来支持fastcgi_pass
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html index.php;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;

                # With php7.0-cgi alone:
                # fastcgi_pass 127.0.0.1:9000;
                # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name example.com;
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}
  1. 测试PHP是否完成安装,编辑/var/www/html/info.php,并添加内容
<?php
    phpinfo();
?>
  1. 访问页面
http://IP/info.php

出现以下页面表示php配置成功

搭建Nginx以及配置https

Nginx 代理nodejs 应用

由于nodejs对文件的处理能力并不是很好,在生产环境中我们一般使用Nginx来处理静态资源以及反向代理。下面用一个小栗子来演示如何用Nginx代理到nodejs编写的服务程序。

vi helloworld.js

var http = require('http');
 
http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('hello world\n');
}).listen(8000);
 
 
console.log('Server running at http://127.0.0.1:8000/');

运行该node程序,并且在8000端口监听,

node helloworld.js

增加conf文件,将proxy_pass 设置为 http://127.0.0.1:8000, 即将所有从8081端口来的请求传递到8000端口,也就是正在运行的node服务。

vi /etc/nginx/sites-enabled/node-test.conf

server {
    listen      8081;
    server_name localhost;
    location / {
        proxy_pass http://127.0.0.1:8000;
    }
}

检查Nginx配置是否正确,

nginx -t

重启Nginx,

nginx -s reload

访问http//ip:8081 , 页面显示hello world

HTTPS

在URL前加https://前缀表明是用SSL加密的,这使得浏览器与服务器之间收发的信息传输将更加安全。

Web服务器启用SSL需要获得一个服务器证书并将该证书与要使用SSL的服务器绑定。

接下来我们将演示如何在本地生成SSL 私钥以及证书,并将本地访问配置成https。

如果在线上使用https,需要向SSL官方网站申请证书。

生成证书

# 进入存放私钥以及证书的目录
cd /etc/nginx/

# 创建私钥,并输入加密私钥的密码
openssl genrsa -des3 -out server.key 1024

# 基于私钥生成csr证书,创建过程中会要求输入一些相关信息
openssl req -new -key server.key -out server.csr

# 在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key

# 最后标记证书使用上述私钥和CSR:
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

  • server.key (私钥)
  • server.crt (证书文件)

配置Nginx

vi /etc/nginx/sites-enabled/https-test.conf

#
# HTTPS server configuration
#
server {
    listen       443;
    server_name  localhost;

    ssl                  on;
    ssl_certificate      /etc/nginx/server.crt;
    ssl_certificate_key  /etc/nginx/server.key;

    ssl_session_timeout  5m;

#    ssl_protocols  SSLv2 SSLv3 TLSv1;
#    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#    ssl_prefer_server_ciphers   on;

    location / {
        root   html;
        index  testssl.html index.html index.htm;
     proxy_redirect off;
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     #proxy_pass http://;
    }
}

# 验证Nginx配置
nginx -t

# 重启Nginx
nginx -s reload

打开https://localhost, 会跳转到相应页面

Nginx 常用命令

# nginx -s reload            # 重新载入配置文件
# nginx -s reopen            # 重启 Nginx
# nginx -s stop              # 停止 Nginx
# nginx -t                   # 检查配置文件