文章目錄
- 前言
- 一、Nginx是什么?
- 二、使用步驟
- 1.下載Nginx
- 2.配置Nginx監聽
- 3.啟動Nginx
- 4.關閉Nginx
- 三、體驗Nginx
- 1、修改Nginx的組態檔
- 2、配置Nginx資訊
- 總結
前言
隨著網站用戶訪問量的不斷增加,網站的回應時間也在慢慢的增加,甚至有的時候會出現網站奔潰的情況,這說明一臺服務器已經滿足不了我們網站的需求了,于是我們就增加多臺服務器,但是如何把這些服務器用統一的域名來管理呢?我的答案是Nginx,本文就向大家簡單介紹Nginx,并且教大家如何簡單入門,
一、Nginx是什么?
Nginx (engine x) 是一個高性能的HTTP和反向代理web服務器,同時也提供了IMAP/POP3/SMTP服務,Nginx是由伊戈爾·賽索耶夫為俄羅斯訪問量第二的Rambler.ru站點(俄文:Рамблер)開發的,第一個公開版本0.1.0發布于2004年10月4日,因它的穩定性、豐富的功能集、簡單的組態檔和低系統資源的消耗而聞名,2011年6月1日,nginx 1.0.4發布,
其主要的特點還有:占有記憶體少,并發能力強,并且Nginx 啟動特別容易,并且幾乎可以做到7*24不間斷運行,即使運行數個月也不需要重新啟動,你還能夠不間斷服務的情況下進行軟體版本的升級,
并且你要知道一個傳統Tomcat能夠承受的并發訪問量大概是800個左右,而根據Nginx官方資料測驗表明Nginx能夠支持高達 50,000 個并發連接數的回應,以此可見Nginx的強大,
二、使用步驟
1.下載Nginx
打開Nginx官網:http://nginx.org/en/download.html,這里我們選擇穩定版本下載即可,

下載完成之后我們會得到一個壓縮包,解壓就可以使用了,

2.配置Nginx監聽
下載完Nginx我們就可以使用了,我們可以先看一下Nginx的組態檔(在conf目錄下有個nginx.conf檔案),


這個組態檔我們可以修改Nginx代理的主機和埠,這里Nginx默認代理的是localhost:80,
3.啟動Nginx
現在我們開啟Nginx,體驗一下,啟動方式有以下幾種:
- 直接雙擊nginx.exe檔案,這種方式會出現一個一閃而過的視窗
- 打開cmd命令控制視窗,切換到nginx目錄下,輸入nginx.exe回車即可(推薦方式)

然后我們就可以去訪問localhost:80了,也直接可訪問localhost,因為瀏覽器訪問埠默認是80,

看到以上頁面說明我們Nginx開啟成功了!
4.關閉Nginx
使用cmd命令視窗啟動nginx,關閉cmd視窗是無法關閉nginx行程的,我們需要使用以下命令來結束nginx行程:
- 重新打開一個cmd視窗,輸入nginx命令
nginx -s stop(快速停止nginx) 或nginx -s quit(完整有序的停止nginx) - 使用taskkill
taskkill /f /t /im nginx.exe
1. taskkill是用來終止行程的,
2. /f是強制終止,
3. /t終止指定的行程和任何由此啟動的子行程,
4. /im示指定的行程名稱,
三、體驗Nginx
通過上面的學習,我們對nginx有了基本的認識,接下來我們實戰進行體驗,我們可以給一個完整的專案配置Nginx,
問題:我們假設現在在我們電腦上分別開放了兩個埠(8080和8081)用于提供服務,現在我們需要使用Nginx將這兩個埠用80代替,即用戶訪問80埠即可,怎么做呢?

1、修改Nginx的組態檔
我們先打開nginx的組態檔(nginx.conf),里面有一大堆配置,看起來很繁瑣,
#user nobody;
worker_processes 1;
#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;
#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的全域配置,目前我們可以不用管,
events {
worker_connections 1024;
} //這一部分是配置Nginx最大連接數量等等,
http{
http的一些配置
upstream ${name}{
//負載均衡的主機埠,就是需要代理的主機,
}
server{
listen xx;
server_name xxx;
// 代理
location /xxx{
//訪問該服務器的xx埠的/xxx路由就會進入到這里,訪問這個location下的資源資訊
}
location /admin{
//訪問該服務器的xx埠的/admin路由就會進入到這里,訪問這個location下的資源資訊
}
}
}
2、配置Nginx資訊
現在我們就開始配置上面那個案例了(用#####囊括的內容是需要我們添加的內容),
#user nobody;
worker_processes 1;
#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;
#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;
####### 配置負載均衡
upstream testName{
#服務器資源
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8081 weight=1;
}
#######
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
###### 代理
proxy_pass http://testName;
######
}
#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訪問本地localhost:80埠即可進入到了我們專案的首頁,
總結
以上就是今天博主分享的內容了,本文僅僅簡單介紹了Nginx的入門使用,而Nginx的強大之處不僅如此,還有跟多的內容需要大家自己去深入學習,
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/379509.html
標籤:其他
上一篇:負載均衡nginx
