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

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

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

1、nginx 文件結(jié)構(gòu)
- 全局塊:配置影響nginx全局的指令。一般有運(yùn)行nginx服務(wù)器的用戶組,nginx進(jìn)程pid存放路徑,日志存放路徑,配置文件引入,允許生成worker process數(shù)等。
- events塊:配置影響nginx服務(wù)器或與用戶的網(wǎng)絡(luò)連接。有每個(gè)進(jìn)程的最大連接數(shù),選取哪種事件驅(qū)動模型處理連接請求,是否允許同時(shí)接受多個(gè)網(wǎng)路連接,開啟多個(gè)網(wǎng)絡(luò)連接序列化等。
- http塊:可以嵌套多個(gè)server,配置代理,緩存,日志定義等絕大多數(shù)功能和第三方模塊的配置。如文件引入,mime-type定義,日志自定義,是否使用sendfile傳輸文件,連接超時(shí)時(shí)間,單連接請求數(shù)等。
- server塊:配置虛擬主機(jī)的相關(guān)參數(shù),一個(gè)http中可以有多個(gè)server。
- location塊:配置請求的路由,以及各種頁面的處理情況。
... # 全局塊。配置影響nginx全局的指令。
events { # events塊。配置影響nginx服務(wù)器或與用戶的網(wǎng)絡(luò)連接。
...
}
http # http塊。可以嵌套多個(gè)server,配置代理,緩存,日志定義等絕大多數(shù)功能和第三方模塊的配置。
{
... # http全局塊
server # server塊。配置虛擬主機(jī)的相關(guān)參數(shù),一個(gè)http中可以有多個(gè)server。
{
... # server全局塊
location [PATTERN] # location塊。配置請求的路由,以及各種頁面的處理情況。
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... # http全局塊
}2、默認(rèn)的配置
##
# 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的基本配置
########### 每個(gè)指令必須有分號結(jié)束。#################
#user administrator administrators; #配置用戶或者組,默認(rèn)為nobody nobody。
#worker_processes 2; #允許生成的進(jìn)程數(shù),默認(rèn)為1
#pid /nginx/pid/nginx.pid; #指定nginx進(jìn)程運(yùn)行文件存放地址
error_log log/error.log debug; #制定日志路徑,級別。這個(gè)設(shè)置可以放入全局塊,http塊,server塊,級別以此為:debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; #設(shè)置網(wǎng)路連接序列化,防止驚群現(xiàn)象發(fā)生,默認(rèn)為on
multi_accept on; #設(shè)置一個(gè)進(jìn)程是否同時(shí)接受多個(gè)網(wǎng)絡(luò)連接,默認(rèn)為off
#use epoll; #事件驅(qū)動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大連接數(shù),默認(rèn)為512
}
http {
include mime.types; #文件擴(kuò)展名與文件類型映射表
default_type application/octet-stream; #默認(rèn)文件類型,默認(rèn)為text/plain
#access_log off; #取消服務(wù)日志
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為日志格式的默認(rèn)值
sendfile on; #允許sendfile方式傳輸文件,默認(rèn)為off,可以在http塊,server塊,location塊。
sendfile_max_chunk 100k; #每個(gè)進(jìn)程每次調(diào)用傳輸數(shù)量不能大于設(shè)定的值,默認(rèn)為0,即不設(shè)上限。
keepalive_timeout 65; #連接超時(shí)時(shí)間,默認(rèn)為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; #錯(cuò)誤頁
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; #設(shè)置默認(rèn)頁
proxy_pass http://mysvr; #請求轉(zhuǎn)向mysvr 定義的服務(wù)器列表
deny 127.0.0.1; #拒絕的ip
allow 172.18.5.54; #允許的ip
}
}
}四、 nginx虛擬主機(jī)配置
#下面是server虛擬主機(jī)的配置段
server
{
listen 80;#監(jiān)聽端口
server_name localhost;#域名
index index.html index.htm index.php;
root /usr/local/webserver/nginx/html;#站點(diǎn)目錄
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;
}驗(yàn)證配置文件:
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 : 用來記錄訪問時(shí)間與時(shí)區(qū);
- $request : 用來記錄請求的url與http協(xié)議;
- $status : 用來記錄請求狀態(tài);成功是200;
- $body_bytes_s ent :記錄發(fā)送給客戶端文件主體內(nèi)容大?。?/li>
- $http_referer :用來記錄從那個(gè)頁面鏈接訪問過來的;
- $http_user_agent :記錄客戶端瀏覽器的相關(guān)信息;
訪問鏈接是: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服務(wù)器配置
首先,Nginx是一個(gè)HTTP服務(wù)器,可以將服務(wù)器上的靜態(tài)文件(如HTML、圖片)通過HTTP協(xié)議展現(xiàn)給客戶端。
配置:
server {
listen 80; # 端口
server_name localhost 192.168.1.100; # 域名
location / { # 代表這是項(xiàng)目根目錄
root /usr/share/nginx/www; # 虛擬目錄
}
}2、反向代理服務(wù)器配置
什么是反向代理?
客戶端本來可以直接通過HTTP協(xié)議訪問某網(wǎng)站應(yīng)用服務(wù)器,如果網(wǎng)站管理員在中間加上一個(gè)Nginx,客戶端請求Nginx,Nginx請求應(yīng)用服務(wù)器,然后將結(jié)果返回給客戶端,此時(shí)Nginx就是反向代理服務(wù)器。

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

負(fù)載均衡配置:
upstream myapp {
server 192.168.0.111:8080; # 應(yīng)用服務(wù)器1
server 192.168.0.112:8080; # 應(yīng)用服務(wù)器2
}
server {
listen 80;
location / {
proxy_pass http://myweb;
}
}4、虛擬主機(jī)配置
有的網(wǎng)站訪問量大,需要負(fù)載均衡。然而并不是所有網(wǎng)站都如此出色,有的網(wǎng)站,由于訪問量太小,需要節(jié)省成本,將多個(gè)網(wǎng)站部署在同一臺服務(wù)器上。
例如將www.aaa.com和www.bbb.com兩個(gè)網(wǎng)站部署在同一臺服務(wù)器上,兩個(gè)域名解析到同一個(gè)IP地址,但是用戶通過兩個(gè)域名卻可以打開兩個(gè)完全不同的網(wǎng)站,互相不影響,就像訪問兩個(gè)服務(wù)器一樣,所以叫兩個(gè)虛擬主機(jī)。
虛擬主機(jī)配置:
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; # 對應(yīng)端口號8080
}
}
server {
listen 80;
server_name www.bbb.com; # www.bbb.com域名
location / {
proxy_pass http://localhost:8081; # 對應(yīng)端口號8081
}
}在服務(wù)器8080和8081分別開了一個(gè)應(yīng)用,客戶端通過不同的域名訪問,根據(jù)server_name可以反向代理到對應(yīng)的應(yīng)用服務(wù)器。
虛擬主機(jī)的原理是通過HTTP請求頭中的Host是否匹配server_name來實(shí)現(xiàn)的,有興趣的同學(xué)可以研究一下HTTP協(xié)議。
另外,server_name配置還可以過濾有人惡意將某些域名指向你的主機(jī)服務(wù)器。
到此這篇關(guān)于Ubuntu下搭建與配置Nginx服務(wù)的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx基礎(chǔ)學(xué)習(xí)之realip模塊的使用方法
這篇文章主要給大家介紹了關(guān)于Nginx基礎(chǔ)學(xué)習(xí)之realip模塊使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Nginx具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Nginx 正向代理和反向代理的配置實(shí)現(xiàn)
Nginx通過優(yōu)秀的架構(gòu)設(shè)計(jì)和高效的算法實(shí)現(xiàn)了高性能和高可靠性,本文主要介紹了Nginx 正向代理和反向代理的配置實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-06-06
nginx中的正則表達(dá)式及l(fā)ocation和rewrite總結(jié)
rewrite功能就是,使用nginx提供的全局變量或自己設(shè)置的變量,結(jié)合正則表達(dá)式和標(biāo)記位實(shí)現(xiàn)URL重寫以及重定向,這篇文章主要介紹了nginx中的正則表達(dá)式及l(fā)ocation和rewrite總結(jié),需要的朋友可以參考下2023-12-12
Nginx中的root&alias文件路徑及索引目錄配置詳解
這篇文章主要介紹了Nginx中的root&alias文件路徑及索引目錄配置,順帶講解了root和alias命令的用法,需要的朋友可以參考下2016-01-01
關(guān)于nginx 實(shí)現(xiàn)jira反向代理的問題
這篇文章主要介紹了關(guān)于nginx 實(shí)現(xiàn)jira反向代理的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09

