Ubuntu下搭建與配置Nginx服務
一、Nginx
Nginx("engine x")是一款是由俄羅斯的程序設計師Igor Sysoev所開發(fā)高性能的 Web和 反向代理 服務器,也是一個 IMAP/POP3/SMTP 代理服務器。
三大WEB服務器:apache, Nginx, lighttpd之一。在高連接并發(fā)的情況下,Nginx是Apache服務器不錯的替代品。
nginx應用場合
- 靜態(tài)服務器。(圖片,視頻服務)另一個是lighttpd。并發(fā)幾萬,html,js,css,flv,jpg,gif等。
- 動態(tài)服務,nginx——fastcgi 的方式運行PHP,jsp。(PHP并發(fā)在500-1500,MySQL 并發(fā)在300-1500)。
- 反向代理,負載均衡。日pv2000W以下,都可以直接用nginx做代理。
- 緩存服務。類似 SQUID,VARNISH。
官方網(wǎng)址:http://nginx.org/en/download.html
官網(wǎng)提供三種版本:
Nginx官網(wǎng)提供了三個類型的版本
Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以說是開發(fā)版
Stable version:最新穩(wěn)定版,生產環(huán)境上建議使用的版本
Legacy versions:遺留的老版本的穩(wěn)定版

二、nginx服務搭建
1、使用apt安裝
sudo apt install nginx
2、安裝后的位置:
- /usr/sbin/nginx:主程序
- /etc/nginx:存放配置文件
- /usr/share/nginx:存放靜態(tài)文件
- /var/log/nginx:存放日志
3、啟動并驗證效果
service nginx start # 啟動nginx service nginx reload # 重新加載nginx配置文件
在瀏覽器輸入你的ip地址,如果出現(xiàn)Wellcome to nginx 那么就是配置成功。
另外兩個命令
nginx -s reopen # 重啟 Nginx nginx -s stop # 停止 Nginx

4、查看版本號:
~$ nginx -v nginx version: nginx/1.14.0 (Ubuntu)
三、nginx配置文件介紹

1、nginx 文件結構
- 全局塊:配置影響nginx全局的指令。一般有運行nginx服務器的用戶組,nginx進程pid存放路徑,日志存放路徑,配置文件引入,允許生成worker process數(shù)等。
- events塊:配置影響nginx服務器或與用戶的網(wǎng)絡連接。有每個進程的最大連接數(shù),選取哪種事件驅動模型處理連接請求,是否允許同時接受多個網(wǎng)路連接,開啟多個網(wǎng)絡連接序列化等。
- http塊:可以嵌套多個server,配置代理,緩存,日志定義等絕大多數(shù)功能和第三方模塊的配置。如文件引入,mime-type定義,日志自定義,是否使用sendfile傳輸文件,連接超時時間,單連接請求數(shù)等。
- server塊:配置虛擬主機的相關參數(shù),一個http中可以有多個server。
- location塊:配置請求的路由,以及各種頁面的處理情況。
... # 全局塊。配置影響nginx全局的指令。
events { # events塊。配置影響nginx服務器或與用戶的網(wǎng)絡連接。
...
}
http # http塊。可以嵌套多個server,配置代理,緩存,日志定義等絕大多數(shù)功能和第三方模塊的配置。
{
... # http全局塊
server # server塊。配置虛擬主機的相關參數(shù),一個http中可以有多個server。
{
... # server全局塊
location [PATTERN] # location塊。配置請求的路由,以及各種頁面的處理情況。
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... # http全局塊
}2、默認的配置
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
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;
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 PHP scripts to FastCGI server
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
#}
# 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;
# }
#}3、nginx的基本配置
########### 每個指令必須有分號結束。#################
#user administrator administrators; #配置用戶或者組,默認為nobody nobody。
#worker_processes 2; #允許生成的進程數(shù),默認為1
#pid /nginx/pid/nginx.pid; #指定nginx進程運行文件存放地址
error_log log/error.log debug; #制定日志路徑,級別。這個設置可以放入全局塊,http塊,server塊,級別以此為:debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; #設置網(wǎng)路連接序列化,防止驚群現(xiàn)象發(fā)生,默認為on
multi_accept on; #設置一個進程是否同時接受多個網(wǎng)絡連接,默認為off
#use epoll; #事件驅動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大連接數(shù),默認為512
}
http {
include mime.types; #文件擴展名與文件類型映射表
default_type application/octet-stream; #默認文件類型,默認為text/plain
#access_log off; #取消服務日志
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定義格式
access_log log/access.log myFormat; #combined為日志格式的默認值
sendfile on; #允許sendfile方式傳輸文件,默認為off,可以在http塊,server塊,location塊。
sendfile_max_chunk 100k; #每個進程每次調用傳輸數(shù)量不能大于設定的值,默認為0,即不設上限。
keepalive_timeout 65; #連接超時時間,默認為75s,可以在http,server,location塊。
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #熱備
}
error_page 404 https://www.baidu.com; #錯誤頁
server {
keepalive_requests 120; #單連接請求上限次數(shù)。
listen 80; #監(jiān)聽端口
server_name 127.0.0.1; #監(jiān)聽地址
location ~*^.+$ { #請求的url過濾,正則匹配,~為區(qū)分大小寫,~*為不區(qū)分大小寫。
#root path; #根目錄
#index vv.txt; #設置默認頁
proxy_pass http://mysvr; #請求轉向mysvr 定義的服務器列表
deny 127.0.0.1; #拒絕的ip
allow 172.18.5.54; #允許的ip
}
}
}四、 nginx虛擬主機配置
#下面是server虛擬主機的配置段
server
{
listen 80;#監(jiān)聽端口
server_name localhost;#域名
index index.html index.htm index.php;
root /usr/local/webserver/nginx/html;#站點目錄
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 30d;
#access_log off;
}
location ~ .*\.(js|css)?$
{
expires 15d;
#access_log off;
}
access_log off;
}驗證配置文件:
root@ubuntu: nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
五、nginx全局變量
- $remote_addr 與 $http_x_forwarded_for 用以記錄客戶端的ip地址;
- $remote_user :用來記錄客戶端用戶名稱;
- $time_local : 用來記錄訪問時間與時區(qū);
- $request : 用來記錄請求的url與http協(xié)議;
- $status : 用來記錄請求狀態(tài);成功是200;
- $body_bytes_s ent :記錄發(fā)送給客戶端文件主體內容大小;
- $http_referer :用來記錄從那個頁面鏈接訪問過來的;
- $http_user_agent :記錄客戶端瀏覽器的相關信息;
訪問鏈接是:http://localhost:88/test1/test.php 網(wǎng)站路徑是:/var/www/html $host:localhost $server_port:88 $request_uri:<a href="http://localhost:88/test1/test.php" rel="external nofollow" target="_blank">http:</a>//localhost:88/test1/test.php $document_uri:/test1/test.php $document_root:/var/www/html $request_filename:/var/www/html/test1/test.php
六、Nginx主要配置
1、靜態(tài)Http服務器配置
首先,Nginx是一個HTTP服務器,可以將服務器上的靜態(tài)文件(如HTML、圖片)通過HTTP協(xié)議展現(xiàn)給客戶端。
配置:
server {
listen 80; # 端口
server_name localhost 192.168.1.100; # 域名
location / { # 代表這是項目根目錄
root /usr/share/nginx/www; # 虛擬目錄
}
}2、反向代理服務器配置
什么是反向代理?
客戶端本來可以直接通過HTTP協(xié)議訪問某網(wǎng)站應用服務器,如果網(wǎng)站管理員在中間加上一個Nginx,客戶端請求Nginx,Nginx請求應用服務器,然后將結果返回給客戶端,此時Nginx就是反向代理服務器。

反向代理配置:
server {
listen 80;
location / {
proxy_pass http://192.168.0.112:8080; # 應用服務器HTTP地址
}
}既然服務器可以直接HTTP訪問,為什么要在中間加上一個反向代理,不是多此一舉嗎?反向代理有什么作用?繼續(xù)往下看,下面的負載均衡、虛擬主機,都基于反向代理實現(xiàn),當然反向代理的功能也不僅僅是這些。
3、負載均衡配置
當網(wǎng)站訪問量非常大,也攤上事兒了。因為網(wǎng)站越來越慢,一臺服務器已經(jīng)不夠用了。于是將相同的應用部署在多臺服務器上,將大量用戶的請求分配給多臺機器處理。同時帶來的好處是,其中一臺服務器萬一掛了,只要還有其他服務器正常運行,就不會影響用戶使用。Nginx可以通過反向代理來實現(xiàn)負載均衡。

負載均衡配置:
upstream myapp {
server 192.168.0.111:8080; # 應用服務器1
server 192.168.0.112:8080; # 應用服務器2
}
server {
listen 80;
location / {
proxy_pass http://myweb;
}
}4、虛擬主機配置
有的網(wǎng)站訪問量大,需要負載均衡。然而并不是所有網(wǎng)站都如此出色,有的網(wǎng)站,由于訪問量太小,需要節(jié)省成本,將多個網(wǎng)站部署在同一臺服務器上。
例如將www.aaa.com和www.bbb.com兩個網(wǎng)站部署在同一臺服務器上,兩個域名解析到同一個IP地址,但是用戶通過兩個域名卻可以打開兩個完全不同的網(wǎng)站,互相不影響,就像訪問兩個服務器一樣,所以叫兩個虛擬主機。
虛擬主機配置:
server {
listen 80 default_server;
server_name _;
return 444; # 過濾其他域名的請求,返回444狀態(tài)碼
}
server {
listen 80;
server_name www.aaa.com; # www.aaa.com域名
location / {
proxy_pass http://localhost:8080; # 對應端口號8080
}
}
server {
listen 80;
server_name www.bbb.com; # www.bbb.com域名
location / {
proxy_pass http://localhost:8081; # 對應端口號8081
}
}在服務器8080和8081分別開了一個應用,客戶端通過不同的域名訪問,根據(jù)server_name可以反向代理到對應的應用服務器。
虛擬主機的原理是通過HTTP請求頭中的Host是否匹配server_name來實現(xiàn)的,有興趣的同學可以研究一下HTTP協(xié)議。
另外,server_name配置還可以過濾有人惡意將某些域名指向你的主機服務器。
到此這篇關于Ubuntu下搭建與配置Nginx服務的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持本站。
版權聲明:本站文章來源標注為YINGSOO的內容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。
關注官方微信