Web服务-Nginx网页服务
生活随笔
收集整理的這篇文章主要介紹了
Web服务-Nginx网页服务
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Nginx網(wǎng)頁(yè)服務(wù)
- Nginx網(wǎng)頁(yè)服務(wù)
- 一、編譯安裝Nginx服務(wù)
- 1、關(guān)閉防火墻,將安裝Apache所需軟件包傳到/opt目錄下
- 2、安裝依賴(lài)包
- 3、創(chuàng)建運(yùn)行用戶(hù)、組(Nginx 服務(wù)程序默認(rèn)以 nobody 身份運(yùn)行,建議為其創(chuàng)建專(zhuān)門(mén)的用戶(hù)賬號(hào),以便更準(zhǔn)確地控制其訪(fǎng)問(wèn)權(quán)限)
- 4、編譯安裝Nginx
- 5、檢查、啟動(dòng)、重啟、停止 nginx服務(wù)
- 6、添加 Nginx 系統(tǒng)服務(wù)
- 二、認(rèn)識(shí)Nginx服務(wù)的主配置文件 nginx.conf
- 1、全局配置
- 2、I/O 事件配置
- 3、HTTP 配置
- Web 服務(wù)的監(jiān)聽(tīng)配置
- 4、日志格式設(shè)定
- 四、訪(fǎng)問(wèn)狀態(tài)統(tǒng)計(jì)配置
- 1、第一步
- 2、修改 nginx.conf 配置文件,指定訪(fǎng)問(wèn)位置并添加 stub_status 配置
- 3、重啟服務(wù),訪(fǎng)問(wèn)測(cè)試
- 五、基于授權(quán)的訪(fǎng)問(wèn)控制
- 1、生成用戶(hù)密碼認(rèn)證文件
- 2、修改主配置文件相對(duì)應(yīng)目錄,添加認(rèn)證配置項(xiàng)
- 3、重啟服務(wù),訪(fǎng)問(wèn)測(cè)試
- 六、基于客戶(hù)端的訪(fǎng)問(wèn)控制
- 七、基于域名的 Nginx 虛擬主機(jī)
- 1、為虛擬主機(jī)提供域名解析
- 2、為虛擬主機(jī)準(zhǔn)備網(wǎng)頁(yè)文檔
- 3、修改Nginx的配置文件
- 4、重啟服務(wù),訪(fǎng)問(wèn)測(cè)試
- 八、基于IP 的 Nginx 虛擬主機(jī)
- 九、基于端口的 Nginx 虛擬主機(jī)
Nginx網(wǎng)頁(yè)服務(wù)
一、編譯安裝Nginx服務(wù)
1、關(guān)閉防火墻,將安裝Apache所需軟件包傳到/opt目錄下
systemctl stop firewalld systemctl disable firewalld setenforce 0nginx-1.12.0.tar.gz2、安裝依賴(lài)包
yum -y install pcre-devel zlib-devel gcc gcc-c++ make3、創(chuàng)建運(yùn)行用戶(hù)、組(Nginx 服務(wù)程序默認(rèn)以 nobody 身份運(yùn)行,建議為其創(chuàng)建專(zhuān)門(mén)的用戶(hù)賬號(hào),以便更準(zhǔn)確地控制其訪(fǎng)問(wèn)權(quán)限)
useradd -M -s /sbin/nologin nginx4、編譯安裝Nginx
cd /opt tar zxvf nginx-1.12.0.tar.gz -C /opt/cd nginx-1.12.0/ ./configure \ --prefix=/usr/local/nginx \ #指定nginx的安裝路徑 --user=nginx \ #指定用戶(hù)名 --group=nginx \ #指定組名 --with-http_stub_status_module #啟用 http_stub_status_module 模塊以支持狀態(tài)統(tǒng)計(jì)make && make installln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #讓系統(tǒng)識(shí)別nginx的操作命令
5、檢查、啟動(dòng)、重啟、停止 nginx服務(wù)
nginx -t #檢查配置文件是否配置正確 #啟動(dòng) nginx #停止 cat /usr/local/nginx/logs/nginx.pid #先查看nginx的PID號(hào) kill -3 <PID號(hào)> kill -s QUIT <PID號(hào)> killall -3 nginx killall -s QUIT nginx #重載 kill -1 <PID號(hào)> kill -s HUP <PID號(hào)> killall -1 nginx killall -s HUP nginx #日志分隔,重新打開(kāi)日志文件 kill -USR1 <PID號(hào)> #平滑升級(jí) kill -USR2 <PID號(hào)>6、添加 Nginx 系統(tǒng)服務(wù)
方法一: vim /etc/init.d/nginx #!/bin/bash #chkconfig: - 99 20 #description:Nginx Service Control Script COM="/usr/local/nginx/sbin/nginx" PID="/usr/local/nginx/logs/nginx.pid" case "$1" in start)$COM ;;stop)kill -s QUIT $(cat $PID) ;;restart)$0 stop$0 start ;;reload)kill -s HUP $(cat $PID) ;;*) echo "Usage: $0 {start|stop|restart|reload}" exit 1esac exit 0chmod +x /etc/init.d/nginx chkconfig --add nginx #添加為系統(tǒng)服務(wù) systemctl stop nginx systemctl start nginx
二、認(rèn)識(shí)Nginx服務(wù)的主配置文件 nginx.conf
vim /usr/local/nginx/conf/nginx.conf1、全局配置
#user nobody; #運(yùn)行用戶(hù),若編譯時(shí)未指定則默認(rèn)為 nobody worker_processes 1; #工作進(jìn)程數(shù)量,可配置成服務(wù)器內(nèi)核數(shù) * 2 #error_log logs/error.log; #錯(cuò)誤日志文件的位置 #pid logs/nginx.pid; #PID 文件的位置2、I/O 事件配置
events {use epoll; #使用 epoll 模型,2.6及以上版本的系統(tǒng)內(nèi)核,建議使用epoll模型以提高性能worker_connections 4096; #每個(gè)進(jìn)程處理 4096 個(gè)連接 } #如提高每個(gè)進(jìn)程的連接數(shù)還需執(zhí)行“ulimit -n 65535”命令臨時(shí)修改本地每個(gè)進(jìn)程可以同時(shí)打開(kāi)的最大文件數(shù)。 #在Linux平臺(tái)上,在進(jìn)行高并發(fā)TCP連接處理時(shí),最高的并發(fā)數(shù)量都要受到系統(tǒng)對(duì)用戶(hù)單一進(jìn)程同時(shí)可打開(kāi)文件數(shù)量的限制(這是因?yàn)橄到y(tǒng)為每個(gè)TCP連接都要?jiǎng)?chuàng)建一個(gè)socket句柄,每個(gè)socket句柄同時(shí)也是一個(gè)文件句柄)。 #可使用ulimit -a命令查看系統(tǒng)允許當(dāng)前用戶(hù)進(jìn)程打開(kāi)的文件數(shù)限制.3、HTTP 配置
http {##文件擴(kuò)展名與文件類(lèi)型映射表include mime.types;##默認(rèn)文件類(lèi)型default_type application/octet-stream;##日志格式設(shè)定#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';##訪(fǎng)問(wèn)日志位置#access_log logs/access.log main;##支持文件發(fā)送(下載)sendfile on;##此選項(xiàng)允許或禁止使用socke的TCP_CORK的選項(xiàng)(發(fā)送數(shù)據(jù)包前先緩存數(shù)據(jù)),此選項(xiàng)僅在使用sendfile的時(shí)候使用#tcp_nopush on;##連接保持超時(shí)時(shí)間,單位是秒#keepalive_timeout 0;keepalive_timeout 65;##gzip模塊設(shè)置,設(shè)置是否開(kāi)啟gzip壓縮輸出#gzip on;Web 服務(wù)的監(jiān)聽(tīng)配置
server {##監(jiān)聽(tīng)地址及端口listen 80; ##站點(diǎn)域名,可以有多個(gè),用空格隔開(kāi)server_name www.zhangsan.com;##網(wǎng)頁(yè)的默認(rèn)字符集charset utf-8;##根目錄配置location / {##網(wǎng)站根目錄的位置/usr/local/nginx/htmlroot html;##默認(rèn)首頁(yè)文件名index index.html index.php;}##內(nèi)部錯(cuò)誤的反饋?lái)?yè)面error_page 500 502 503 504 /50x.html;##錯(cuò)誤頁(yè)面配置location = /50x.html {root html;}} }4、日志格式設(shè)定
$remote_addr與$http_x_forwarded_for用以記錄客戶(hù)端的ip地址; $remote_user:用來(lái)記錄客戶(hù)端用戶(hù)名稱(chēng); $time_local: 用來(lái)記錄訪(fǎng)問(wèn)時(shí)間與時(shí)區(qū); $request: 用來(lái)記錄請(qǐng)求的url與http協(xié)議; $status: 用來(lái)記錄請(qǐng)求狀態(tài);成功是200, $body_bytes_sent :記錄發(fā)送給客戶(hù)端文件主體內(nèi)容大小; $http_referer:用來(lái)記錄從那個(gè)頁(yè)面鏈接訪(fǎng)問(wèn)過(guò)來(lái)的; $http_user_agent:記錄客戶(hù)瀏覽器的相關(guān)信息; 通常web服務(wù)器放在反向代理的后面,這樣就不能獲取到客戶(hù)的IP地址了,通過(guò)$remote_add拿到的IP地址是反向代理服務(wù)器的iP地址。反向代理服務(wù)器在轉(zhuǎn)發(fā)請(qǐng)求的http頭信息中,可以增加x_forwarded_for信息,用以記錄原有客戶(hù)端的IP地址和原來(lái)客戶(hù)端的請(qǐng)求的服務(wù)器地址。location常見(jiàn)配置指令,root、alias、proxy_pass root(根路徑配置):請(qǐng)求www.kgc.com/test/1.jpg,會(huì)返回文件/usr/local/nginx/html/test/1.jpg alias(別名配置):請(qǐng)求www.kgc.com/test/1.jpg,會(huì)返回文件/usr/local/nginx/html/1.jpg proxy_pass(反向代理配置): proxy_pass http://127.0.0.1:8080/; 會(huì)轉(zhuǎn)發(fā)請(qǐng)求到http://127.0.0.1:8080/1.jpg proxy_pass http://127.0.0.1:8080; 會(huì)轉(zhuǎn)發(fā)請(qǐng)求到http://127.0.0.1:8080/test/1.jpg四、訪(fǎng)問(wèn)狀態(tài)統(tǒng)計(jì)配置
1、第一步
先使用命令/usr/local/nginx/sbin/nginx -V 查看已安裝的 Nginx 是否包含 HTTP_STUB_STATUS 模塊2、修改 nginx.conf 配置文件,指定訪(fǎng)問(wèn)位置并添加 stub_status 配置
cd /usr/local/nginx/conf cp nginx.conf nginx.conf.bak vim /usr/local/nginx/conf/nginx.conf ...... http { ......server {listen 80;server_name www.zhangsanc.com;charset utf-8;location / {root html;index index.html index.php;}##添加 stub_status 配置##location /status { #訪(fǎng)問(wèn)位置為/statusstub_status on; #打開(kāi)狀態(tài)統(tǒng)計(jì)功能access_log off; #關(guān)閉此位置的日志記錄}} }3、重啟服務(wù),訪(fǎng)問(wèn)測(cè)試
systemctl restart nginx瀏覽器訪(fǎng)問(wèn) http://192.168.172.20/status Active connections :表示當(dāng)前的活動(dòng)連接數(shù); server accepts handled requests :表示已經(jīng)處理的連接信息,三個(gè)數(shù)字依次表示已處理的連接數(shù)、成功的TCP握手次數(shù)、 已處理的請(qǐng)求數(shù)。五、基于授權(quán)的訪(fǎng)問(wèn)控制
1、生成用戶(hù)密碼認(rèn)證文件
yum install -y httpd-tools htpasswd -c /usr/local/nginx/passwd.db zhangsan chmod 400 /usr/local/nginx/passwd.db
2、修改主配置文件相對(duì)應(yīng)目錄,添加認(rèn)證配置項(xiàng)
vim /usr/local/nginx/conf/nginx.conf ......server {location / {......##添加認(rèn)證配置##auth_basic "secret";auth_basic_user_file /usr/local/nginx/passwd.db;}}3、重啟服務(wù),訪(fǎng)問(wèn)測(cè)試
nginx -t systemctl restart nginx瀏覽器訪(fǎng)問(wèn) http://192.168.172.20
六、基于客戶(hù)端的訪(fǎng)問(wèn)控制
訪(fǎng)問(wèn)控制規(guī)則如下: deny IP/IP 段:拒絕某個(gè) IP 或 IP 段的客戶(hù)端訪(fǎng)問(wèn)。 allow IP/IP 段:允許某個(gè) IP 或 IP 段的客戶(hù)端訪(fǎng)問(wèn)。 規(guī)則從上往下執(zhí)行,如匹配則停止,不再往下匹配。vim /usr/local/nginx/conf/nginx.conf ......server {location / {......##添加控制規(guī)則##deny 192.168.172.20; #拒絕訪(fǎng)問(wèn)的客戶(hù)端 IPallow all; #允許其它IP客戶(hù)端訪(fǎng)問(wèn)}}systemctl restart nginx
七、基于域名的 Nginx 虛擬主機(jī)
1、為虛擬主機(jī)提供域名解析
方法一:echo "192.168.172.10 www.kgc.com www.benet.com" >> /etc/hosts方法二:做域名解析服務(wù)2、為虛擬主機(jī)準(zhǔn)備網(wǎng)頁(yè)文檔
mkdir -p /var/www/html/benet mkdir -p /var/www/html/kgc echo "<h1>www.kgc.com</h1>" > /var/www/html/kgc/index.html echo "<h1>www.benet.com</h1>" > /var/www/html/benet/index.html3、修改Nginx的配置文件
vim /usr/local/nginx/conf/nginx.conf ...... http { ......server {listen 80;server_name www.zhangsan.com; #設(shè)置域名www.zhangsan.comcharset utf-8;access_log logs/www.zhangsan.access.log; location / {root /var/www/html/zhangsan; #設(shè)置www.zhangsan.com 的工作目錄index index.html index.php;}error_page 500 502 503 504 /50x.html;location = 50x.html{root html;}}server {listen 80;server_name www.lisi.com; #設(shè)置域名www.lisi.comcharset utf-8;access_log logs/www.lisi.access.log; location / {root /var/www/html/lisi;index index.html index.php;}error_page 500 502 503 504 /50x.html;location = 50x.html{root html;}} }4、重啟服務(wù),訪(fǎng)問(wèn)測(cè)試
systemctl restart nginx瀏覽器訪(fǎng)問(wèn) http://www.kgc.com http://www.benet.com
八、基于IP 的 Nginx 虛擬主機(jī)
ifconfig ens33:0 192.168.172.200 netmask 255.255.255.0 vim /usr/local/nginx/conf/nginx.conf ...... http { ......server {listen 192.168.172.20:80; #設(shè)置監(jiān)聽(tīng)地址192.168.172.20server_name www.zhangsan.com;charset utf-8;access_log logs/www.zhangsan.access.log; location / {root /var/www/html/zhangsan;index index.html index.php;}error_page 500 502 503 504 /50x.html;location = 50x.html{root html;}}server {listen 192.168.172.200:80; #設(shè)置監(jiān)聽(tīng)地址192.168.172.200server_name www.lisi.com;charset utf-8;access_log logs/www.lisi.access.log; location / {root /var/www/html/lisi;index index.html index.php;}error_page 500 502 503 504 /50x.html;location = 50x.html{root html;}} }systemctl restart nginx瀏覽器訪(fǎng)問(wèn) http://192.168.172.20 http://192.168.172.200
九、基于端口的 Nginx 虛擬主機(jī)
vim /usr/local/nginx/conf/nginx.conf ...... http { ......server {listen 192.168.172.20:8080; #設(shè)置監(jiān)聽(tīng) 8080 端口server_name www.zhangsan.com;charset utf-8;access_log logs/www.zhangsan.access.log; location / {root /var/www/html/zhangsan;index index.html index.php;}error_page 500 502 503 504 /50x.html;location = 50x.html{root html;}}server {listen 192.168.172.20:8888; #設(shè)置監(jiān)聽(tīng) 8888 端口server_name www.lisi.com;charset utf-8;access_log logs/www.lisi.access.log; location / {root /var/www/html/lisi;index index.html index.php;}error_page 500 502 503 504 /50x.html;location = 50x.html{root html;}} }systemctl restart nginx瀏覽器訪(fǎng)問(wèn) http://192.168.172.20:8080 http://192.168.172.20:8888
總結(jié)
以上是生活随笔為你收集整理的Web服务-Nginx网页服务的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 内存时序:数字世界的秘密密码
- 下一篇: Nginx —— 检查配置文件ngi