Nginx是一款輕量級的HTTP服務器,采用事件驅動的異步非阻塞處理方式框架,這讓其具有極好的IO性能,時常用於服務端的反向代理和負載均衡。
Nginx的優點
- 支持海量高並發:采用IO多路復用epoll。官方測試Nginx能夠支持5萬並發鏈接,實際生產環境中可以支撐2-4萬並發連接數。
- 內存消耗少:在主流的服務器中Nginx目前是內存消耗最小的了,比如我們用Nginx+PHP,在3萬並發鏈接下,開啟10個Nginx進程消耗150M內存。
- 免費使用可以商業化:Nginx為開源軟件,采用的是2-clause BSD-like協議,可以免費使用,並且可以用於商業。
- 配置文件簡單:網絡和程序配置通俗易懂,即使非專業運維也能看懂。
環境:
- VM虛擬機 Centos7.4 64位版本
- Xshell 6終端模擬軟件
1、Nginx版本說明
- Mainline version :開發版,主要是給廣大Nginx愛好者,測試、研究和學習的,但是不建議使用於生產環境。
- Stable version : 穩定版,也就是我們說的長期更新版本。這種版本一般比較成熟,經過長時間的更新測試,所以這種版本也是主流版本。
- legacy version : 歷史版本,如果你需要以前的版本,Nginx也是有提供的。
我的系統已經安裝了Nginx,可以使用如下命令進行版本檢測:
[root@localhost ~]# nginx -v
如果出現以下內容說明nginx安裝成功
[root@localhost ~]# nginx -v
nginx version: nginx/1.14.0
如果你的linux系統中沒有安裝nginx,或者不是最新的版本,那我們可以自行配置yum源,下面是官網提供的源,我們可以放心使用。
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1
復制上面的代碼,然后在終端里面輸入:
vim /etc/yum.repos.d/nginx.repo
然后把代碼復制進去,這里需要vim操作。賦值完成后,你需要修改一下對應的操作系統和版本號,因為我的是centos和7的版本,所以改為這樣。
baseurl=http://nginx.org/packages/centos/7/$basearch/
你可以根據你的系統或需要的版本進行修改。如果都已經准備好了,那就可以開始安裝了,安裝的命令非常簡單:
yum install nginx
安裝完成后,你就可以來檢測一下nginx版本了。
2、Nginx基本配置文件詳解
安裝完成nginx之后,想知道系統中多了哪些文件,安裝到了哪里,我們可以使用下面的命令進行查看:
rpm -ql nginx
rpm 是linux的rpm包管理工具,-q 代表詢問模式,-l 代表返回列表,這樣我們就可以找到nginx的所有安裝位置了。
[root@localhost ~]# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/rc.d/init.d/nginx
/etc/rc.d/init.d/nginx-debug
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/sbin/nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.14.0
/usr/share/doc/nginx-1.14.0/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx
nginx.conf文件解讀
nginx文件是Nginx的總配置文件,在我們搭建服務器時經常調整的文件。
使用如下命令打開nginx.conf文件
vim /etc/nginx/nginx.conf
下面是文件的注釋
# 運行用戶,默認是nginx,可以不進行設置
user nginx;
#Nginx進程,一般設置和cpu核數一樣
worker_processes 1;
#錯誤日志存放位置
error_log /var/log/nginx/error.log warn;
#進程pid存放位置
pid /var/run/nginx.pid;
events {
worker_connections 1024;#單個后台進程的最大並發數
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;#nginx訪問日志的存放位置
sendfile off;#是否開啟高效傳輸模式 on開啟 off關閉
#tcp_nopush on;#減少網絡報文段的數量
keepalive_timeout 65; #保持連接的時間,也叫超時時間
#gzip on;#開啟gzip壓縮模式
include /etc/nginx/conf.d/*.conf;#包含的子配置項的位置和文件
}
default.conf配置項詳解
在nginx.conf配置項文件里面的最后一行,我們打開inclue子文件目錄里面都是些什么內容,里面有些配置文件是我自己新建的。
[root@localhost conf.d]# ls
default.conf default.conf.bak d
efault.conf.rpmnew quickapp-local.conf ssl.conf test-8081.conf test-8082.conf theme.crt theme.csr theme.key theme_nopass.key
然后使用cat default.conf進行查看
server {
listen 80; #配置監聽端口
server_name localhost; //配置域名
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html; #服務默認啟動目錄
index index.html index.htm; #默認訪問文件
}
#error_page 404 /404.html; # 配置404頁面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #錯誤狀態碼的顯示頁面,配置后需要重啟
location = /50x.html {
root /usr/share/nginx/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;
#}
}
明白了這些配置項,我們知道我們的服務目錄放在了/usr/share/nginx/html下,可以使用命令進入看一下目錄下的文件。
[root@localhost html]# ls
50x.html index.html
到這里我們的nginx服務器已經可以為html提供服務器了。我們可以打開瀏覽器,訪問ip地址試一試。
3、Nginx服務啟動、停止、重啟
啟動nginx服務
默認情況下,nginx是不會自動啟動的,需要我們手動啟動。在centos7版本里面,我們可以直接使用nginx命令進行啟動服務,如果不行,那就要使用其他的命令啟動了,我這里只使用nginx命令
nginx
輸入命令后,沒有任何提示,那我們如何知道Nginx服務已經啟動了哪?可以使用Linux的組合命令,進行查詢服務的運行狀況。
[root@localhost ~]# ps aux | grep nginx
root 2056 0.0 0.4 49816 4092 ? Ss Nov01 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 2548 0.0 0.3 49820 3312 ? S Nov01 0:00 nginx: worker process
root 6433 0.0 0.0 103260 840 pts/0 S+ 11:19 0:00 grep nginx
如果出現以上的內容,說明我們的Nginx被正常開啟了。
停止Nginx服務的四種方法
立即停止服務:
nginx -s stop
這個方法強硬,無論是否在工作,都直接停止進程
從容停止服務:
nginx -s quit
這種方法較stop相比就比較溫和一些了,需要進程完成當前工作后再停止。
killall殺死進程:
這種方法也是比較野蠻的,我們直接殺死進程,但是在上面使用沒有效果時,我們用這種方法還是比較好的。
killall nginx
systemctl停止:
systemctl stop nginx.service
重啟nginx服務:
nginx -s reopen
或者
systemctl restart nginx.service
重新載入配置文件,在修改了配置文件之后,都需要進行這個操作,才能生效
nginx -s reload
查看端口號
在默認情況下,Nginx啟動后會監聽80端口,從而提供HTTP訪問,如果80端口已經被占用則會啟動失敗。我么可以使用netstat -tlnp命令查看端口號的占用情況。