Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。在高连接并发的情况下,Nginx与Apache相比,Nginx占用资源更少,并且配置更加灵活,轻量。
Ubuntu 16.04 i386
我们将使用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, 显示以下页面表示安装成功
如果不能访问,也不要着急,先看看返回什么错误,一般有下面错误:
端口80被占用: Starting nginx: [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
解决方法:找到占用80端口的进程,干掉该进程,然后重启Nginx
403 error
解决方法:一般是Nginx配置文件里的网站路径不正确,可以用nginx -t
验证一下配置信息
Nginx只是一个web服务器,默认不支持php,如果要打开php编写的服务器,需要通过php-fpm(管理fastcgi 的进程)来处理请求。
nginx和php-fpm可以通过监听9000端口或者socket(默认)来实现。
sudo apt-get install php7.0-fpm
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;
# }
#}
<?php
phpinfo();
?>
http://IP/info.php
出现以下页面表示php配置成功
由于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
在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
vi /etc/nginx/sites-enabled/https-test.conf
#
# HTTPS server configuration
#
server {
listen 443 ssl http2;
listen [::]:443 ssl http2 ipv6only=on; ## listen for ipv6
root /var/www/html;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name _;
ssl_certificate /etc/nginx/server.crt;
ssl_certificate_key /etc/nginx/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
# Make site accessible from http://localhost/
server_name _;
# Disable sendfile as per https://docs.vagrantup.com/v2/synced-folders/virtualbox.html
sendfile off;
# Add stdout logging
error_log /dev/stdout info;
access_log /dev/stdout;
# Add option for x-forward-for (real ip when behind elb)
#real_ip_header X-Forwarded-For;
#set_real_ip_from 172.16.0.0/12;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = /404.html {
root /var/www/errors;
internal;
}
location ^~ /ngd-style.css {
alias /var/www/errors/style.css;
access_log off;
}
location ^~ /ngd-sad.svg {
alias /var/www/errors/sad.svg;
access_log off;
}
# pass the PHP scripts to FastCGI server listening on socket
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
location ~* \.(jpg|jpeg|gif|png|css|js|ico|webp|tiff|ttf|svg)$ {
expires 5d;
}
# 验证Nginx配置
nginx -t
# 重启Nginx
nginx -s reload
打开https://localhost, 会跳转到相应页面
# nginx -s reload # 重新载入配置文件
# nginx -s reopen # 重启 Nginx
# nginx -s stop # 停止 Nginx
# nginx -t # 检查配置文件