? ? ? ? Nginx ("engine x") 是一個(gè)高性能的 HTTP 和 反向代理 服務(wù)器蟆淀,也是一個(gè) IMAP/POP3/SMTP 代理服務(wù)器。 Nginx 是由 Igor Sysoev 為俄羅斯訪(fǎng)問(wèn)量第二的 Rambler.ru 站點(diǎn)開(kāi)發(fā)的闽晦,第一個(gè)公開(kāi)版本0.1.0發(fā)布于2004年10月4日扳碍。其將源代碼以類(lèi)BSD許可證的形式發(fā)布,因它的穩(wěn)定性仙蛉、豐富的功能集笋敞、示例配置文件和低系統(tǒng)資源的消耗而聞名。
一荠瘪、采用apt-get命令行方式安裝
安裝步驟有以下幾點(diǎn):
1.安裝gcc g++的依賴(lài)庫(kù)
sudo apt-get install build-essential
sudo apt-get install libtool
2.安裝pcre依賴(lài)庫(kù)
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
3.安裝zlib依賴(lài)庫(kù)
sudo apt-get install zlib1g-dev
4.安裝ssl依賴(lài)庫(kù)
sudo apt-get install openssl
5.安裝nginx
sudo apt-get install nginx
二夯巷、配置代理服務(wù)器
內(nèi)部http服務(wù)器端口為8080,websocket服務(wù)器端口為8081哀墓,通過(guò)以下配置后外部訪(fǎng)問(wèn)方式為:
https請(qǐng)求:https://www.example.com/xxxxx
websocket請(qǐng)求:wss://www.example.com/socket
在nginx配置目錄下建立配置文件 sudo nano /etc/nginx/conf.d/example.conf趁餐,配置文件如下
server {
? ? ? ? listen 80; #http端口
? ? ? ? listen 443 ssl; #https端口支持ssl
? ? ? ? ssl_certificate? ? example.crt;? #ssl證書(shū)
? ? ? ? ssl_certificate_key? example.key; #ssl證書(shū)密鑰
? ? ? ? keepalive_timeout? 70;? #
? ? ? ? server_name www.example.com;? #服務(wù)器域名
? ? ? ? #禁止在header中出現(xiàn)服務(wù)器版本,防止黑客利用版本漏洞攻擊
? ? ? ? server_tokens off;
? ? ? ? #如果是全站 HTTPS 并且不考慮 HTTP 的話(huà)篮绰,可以加入 HSTS 告訴你的瀏覽器本網(wǎng)站全站加密后雷,并且強(qiáng)制用 HTTPS 訪(fǎng)問(wèn)
? ? ? ? #add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
? ? ? ? #add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
? ? ? ? #Diffie-Hellman for TLS
? ? ? ? #openssl dhparam -out /etc/nginx/dhparams.pem 2048
? ? ? ? ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
? ? ? ? ssl_prefer_server_ciphers on;
? ? ? ? ssl_dhparam /etc/nginx/dhparams.pem;
? ? ? ? # ......
? ? ? ? fastcgi_param? HTTPS? ? ? ? ? ? ? on;
? ? ? ? fastcgi_param? HTTP_SCHEME? ? ? ? https;
? ? ? ? access_log? ? nginx-access.log;
? ? ? ? error_log? nginx-error.log;
? ? ? ? location / {
? ? ? ? ? ? proxy_pass http://localhost:8080; #內(nèi)部服務(wù)器
? ? ? ? ? ? proxy_redirect? ? off;
? ? ? ? ? ? proxy_set_header X-Real-IP $remote_addr;
? ? ? ? ? ? proxy_set_header Host $host;
? ? ? ? ? ? proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? ? ? ? proxy_http_version 1.1;
? ? ? ? ? ? proxy_set_header Upgrade $http_upgrade;
? ? ? ? ? ? proxy_set_header Connection "upgrade";
? ? ? ? ? ? #proxy_set_header Access-Control-Allow-Origin *;
? ? ? ? ? ? #proxy_set_header Access-Control-Allow-Credentials true;
? ? ? ? }
? ? ? ? #websocket 支持
? ? ? ? location /socket {
? ? ? ? ? ? proxy_pass http://localhost:8081;
? ? ? ? ? ? proxy_redirect? ? off;
? ? ? ? ? ? proxy_set_header X-Real-IP $remote_addr;
? ? ? ? ? ? proxy_set_header Host $host;
? ? ? ? ? ? proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? ? ? ? proxy_http_version 1.1;
? ? ? ? ? ? proxy_set_header Upgrade $http_upgrade;
? ? ? ? ? ? proxy_set_header Connection "upgrade";
? ? ? ? }
}
ubuntu搭建express+node+mongodb(mongoose)