nginx

nginx

nginx

Nginx (engine x) 是一個高性能的HTTP反向代理服務,也是一個IMAP/POP3/SMTP服務。Nginx是由伊戈爾·賽索耶夫爲俄羅斯訪問量第二的Rambler.ru站點(俄文:Рамблер)開發的,第一個公開版本0.1.0發佈於2004年10月4日。

其將源代碼以類BSD許可證的形式發佈,因它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名。2011年6月1日,nginx 1.0.4發佈。

Nginx是一款輕量級Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。其特點是佔有內存少,併發能力強,事實上nginx的併發能力確實在同類型的網頁服務器中表現較好,中國大陸使用nginx網站用戶有:百度、京東新浪網易騰訊淘寶

http狀態碼:200 一切正常

                         400請求語法錯誤

   401訪問被拒絕

   403資源不可用

   404無法找到指定資源

   500服務器內部錯誤

 

nginx編譯安裝

停止原有web服務器:lsof -i:80 && systemctl  stop httpd

首先,在瀏覽器中輸入nginx.org回車進入nginx的官網,然後單擊網站右側的download進入下載頁面,複製nginx-1.8.1版的網址,再執行如下命令。

cd

wget  http://nginx.org/download/nginx-1.8.1.tar.gz

創建普通用戶來運行nginx:(nginx有一個屬於自己的賬號)

useradd  nginx  -M  -s /sbin/nologin    -M不指定家目錄

yum  -y  install  gcc  pcre-devel  openssl-devel 安裝gcc編譯器和相關的包

 

解壓並安裝nginx:

tar  xf   nginx-1.8.1.tar.gz

cd  nginx-1.8.1

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module 生成編譯安裝的Makefile環境配置文件

make  && make  install

------------

--prefix=/usr/local/nginx  \\指定安裝路徑

--with-http_stub_status_module \\聲明啓用server status服務狀態頁,默認不啓用

--with-http_ssl_module \\啓用ssl(Secure Sockets Layer安全套接層)模塊,以支持httpds請求

--sbin-path=/usr/sbin \\聲明nginx命令目錄

 

啓動:

ln -s /usr/local/nginx/sbin/nginx  /usr/bin/nginx

nginx -t 檢測nginx配置文件的語法

nginx  啓動nginx服務

lsof -i:80 或netstat -atunlp |grep :80

 

查看命令幫助

/usr/local/nginx/sbin/nginx -h

-v  查看nginx版本

-V  查看編譯參數

-t  測試默認配置文件

-c  加載非默認配置文件,默認配置文件是/usr/local/nginx/cong/nginx.conf

-s  發送信號(signal)給nginx的master主進程,信號可以是stop,quit,reoen,reload

 

nginx文件解說:

nginx的工作目錄:/usr/local/nginx/

主配置文件:/usr/local/nginx/conf/nginx.conf

默認主頁目錄:/usr/local/nginx/html

默認主頁:/usr/local/nginx/html/index.html

50x錯誤提示目錄:/usr/local/nginx/html/50x.html

日誌文件目錄:/usr/local/nginx/logs

 

echo nihao > /usr/local/nginx/html/index.html

curl  127.0.0.1

elinks  127.0.0.1

 

nginx配置文件詳解

nginx主配置文件主要有以下幾大塊
1、全局塊:配置影響nginx全局的指令。一般有運行nginx服務器的用戶組,nginx進程pid存放路徑,日誌存放路徑,配置文件引入,允許生成worker process數等。
2、events(事件)塊:配置影響nginx服務器或與用戶的網絡連接。有每個進程的最大連接數,選取哪種事件驅動模型處理連接請求,是否允許同時接受多個網路連接,開啓多個網絡連接序列化等。
3、http塊:可以嵌套多個server(可用於配置web虛擬主機),配置代理,緩存,日誌定義等絕大多數功能和第三方模塊的配置。如文件引入,mime-type定義,日誌自定義,是否使用sendfile傳輸文件,連接超時時間,單連接請求數等。
4、server(服務器)塊:配置虛擬主機的相關參數,一個http中可以有多個server。
5、location(定位)塊:配置請求的路由(用於做反向代理、正向代理),以及各種頁面的處理情況。

 

#user nobody; #nginx用戶及組,如果用戶和組名一樣可只寫一個 
worker_processes 1; #定義了nginx對外提供web服務時的worker進程數。
#最優值取決於許多因素,包括(但不限於)CPU核的數量、存儲數據的硬盤數量及負載模式。
#不能確定的時候,將其設置爲可用的CPU核心數將是一個好的開始(設置爲“auto”將嘗試自動檢測它)
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; 
#pid logs/nginx.pid; 
events {
worker_connections 1024; #每個進程的最大連接數,根據需要調整大小
}
http {
include mime.types; 
#文件擴展名與文件類型映射表
default_type application/octet-stream;

server_tokens off; 
#隱藏軟件版本號

#log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 
#定義訪問日誌格式
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main; 
#定義日誌文件
sendfile on; #開啓高效文件傳輸模式
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; #連接超時時間
#gzip on;
server {
listen 80;
server_name localhost;

#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}

#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}

nginx虛擬主機

虛擬主機,就是把一臺物理服務器劃分成多個“虛擬”的服務器,每一個虛擬主機都可以有獨立的域名和獨立的目錄

基於ip的虛擬主機

 

準備工作:準備網站主頁目錄和主頁,臨時加2個網卡

ifconfig  ens33:1  192.168.11.111/24  up

ifconfig  ens33:2  192.168.11.112/24  up

mkdir  /web/qf

mkdir  /12

echo  www.qf.com  >  /web/qf/index.html

echo  nihao  > /12/index.html

mkdir  /usr/local/nginx/conf.d

sed  -i  18a\include  /usr/local/nginx/conf.d/*.conf;  /usr/local/nginx/conf/nginx.conf

vim  /usr/local/nginx/conf.d/ip.conf

基於端口的虛擬主機

基於域名的虛擬主機

vim /usr/local/nginx/conf.d/qf/conf

vim  /etc/hosts

hosts裏的域名要和qf.conf裏的域名一樣

nginx訪問控制

  有時我們會有這麼一種需求,就是你的網站並不想提供一個公共的訪問或者某些頁面不希望公開,我們希望的是某些特定的客戶端可以訪問,那麼我們可以在訪問是要求進行身份認證,就如給你自己的家門加一把鎖,以拒絕那些不速之客,我們在服務課程中學習過apache訪問控制,對於nginx來說同樣可以實現,並且整個過程和apache非常的相識。

 用戶認證:

location / {

   root  /11;

   index  index.html  index.htm;

   auth_basic  haha;

   auth_basic_user_file  /usr/local/nginx/passwd.db;

}

apache認證用戶賬號的創建工具:htpasswd [查  which  htpasswd]

  查htpasswd文件由那哪個包提供:yum  provides  htpasswd

安裝htpasswd工具的軟件:yum  -y  install  httpd-tools

htpasswd  -c  /usr/local/nginx/passwd.db  lucy 創建Lucy用戶,密碼爲0

cat  /usr/local/nginx/passwd.db

 

-c 創建新的htpasswd賬號文件,僅用於第1次
-m 以MD5方式加密用戶密碼
-D 刪除指定的用戶賬號

nginx  -t

nginx  -s  reload

 

訪問控制ip

 

nginx  -t

nginx  -s  reload

就是隻允許12這個ip可以訪問

nginx反向代理和負載均衡

通常的代理服務器,只用於代理內部網絡對Internet的連接請求,客戶機必須指定代理服務器,並將本來要直接發送到Web服務器上的http請求發送到代理服務器中由代理服務器向Internet上的web服務器發起請求,最終達到客戶機上網的目的(也就是正向代理)。

 

而反向代理(Reverse Proxy)方式是指以代理服務器來接受internet上的連接請求,然後將請求轉發給內部網絡上的服務器,並將從服務器上得到的結果返回給internet上請求連接的客戶端,此時代理服務器對外就表現爲一個反向代理服務器。

 

負載均衡建立在現有網絡結構之上,它提供了一種廉價有效透明的方法擴展網絡設備和服務器的帶寬、增加吞吐量、加強網絡數據處理能力、提高網絡的靈活性和可用性。

 

負載均衡,英文名稱爲Load Balance,其意思就是分攤到多個操作單元上進行執行,例如Web服務器、FTP服務器、企業關鍵應用服務器和其它關鍵任務服務器等,從而共同完成工作任務。

反向代理

先新建目錄mkdir /usr/local/nginx/conf.d

在主配置文件中/usr/local/nginx/conf/nginx.conf中加入include  /usr/local/nginx/conf.d/*.conf;

再加一個網卡ifconfig  ens33:1  10.0.0.20/24 up

在/usr/local/nginx/conf.d/proxy.conf中

nginx  -t  &&  nginx  -s  reload

測試 curl   10.0.0.20

 

負載均衡測試

代理服務器

apache服務器

先加3張網卡

ifconfig  ens33:1   10.0.0.21/24  up

ifconfig  ens33:2   10.0.0.22/24  up

ifconfig  ens33:3   10.0.0.23/24  up

cd  /var/www/html

mkdir  html  www  mail

echo  qf.com  >  html/index.html

echo  nihao  >  www/index.html

echo  www.qf.com  >  mail/index.html

vim  /etc/httpd/conf.d/qf.conf

測試

然後在11上

mkdir  -v  /usr/local/nginx/conf.d

vim  /usr/local/nginx/conf.d/qf/conf

image.png

加入這幾行代碼。

在主配置文件中/usr/local/nginx/conf/nginx.conf加入

image.png

使測試的時候能夠讀到路徑。

最後測試

image.png

這樣就成功了

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章