日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Nginx >内容正文

Nginx

Web服务-Nginx网页服务

發(fā)布時間:2024/2/28 Nginx 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Web服务-Nginx网页服务 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Nginx網(wǎng)頁服務(wù)

  • Nginx網(wǎng)頁服務(wù)
  • 一、編譯安裝Nginx服務(wù)
    • 1、關(guān)閉防火墻,將安裝Apache所需軟件包傳到/opt目錄下
    • 2、安裝依賴包
    • 3、創(chuàng)建運(yùn)行用戶、組(Nginx 服務(wù)程序默認(rèn)以 nobody 身份運(yùn)行,建議為其創(chuàng)建專門的用戶賬號,以便更準(zhǔn)確地控制其訪問權(quán)限)
    • 4、編譯安裝Nginx
    • 5、檢查、啟動、重啟、停止 nginx服務(wù)
    • 6、添加 Nginx 系統(tǒng)服務(wù)
  • 二、認(rèn)識Nginx服務(wù)的主配置文件 nginx.conf
    • 1、全局配置
    • 2、I/O 事件配置
    • 3、HTTP 配置
      • Web 服務(wù)的監(jiān)聽配置
    • 4、日志格式設(shè)定
  • 四、訪問狀態(tài)統(tǒng)計(jì)配置
    • 1、第一步
    • 2、修改 nginx.conf 配置文件,指定訪問位置并添加 stub_status 配置
    • 3、重啟服務(wù),訪問測試
  • 五、基于授權(quán)的訪問控制
    • 1、生成用戶密碼認(rèn)證文件
    • 2、修改主配置文件相對應(yīng)目錄,添加認(rèn)證配置項(xiàng)
    • 3、重啟服務(wù),訪問測試
  • 六、基于客戶端的訪問控制
  • 七、基于域名的 Nginx 虛擬主機(jī)
    • 1、為虛擬主機(jī)提供域名解析
    • 2、為虛擬主機(jī)準(zhǔn)備網(wǎng)頁文檔
    • 3、修改Nginx的配置文件
    • 4、重啟服務(wù),訪問測試
  • 八、基于IP 的 Nginx 虛擬主機(jī)
  • 九、基于端口的 Nginx 虛擬主機(jī)

Nginx網(wǎng)頁服務(wù)

一、編譯安裝Nginx服務(wù)

1、關(guān)閉防火墻,將安裝Apache所需軟件包傳到/opt目錄下

systemctl stop firewalld systemctl disable firewalld setenforce 0nginx-1.12.0.tar.gz

2、安裝依賴包

yum -y install pcre-devel zlib-devel gcc gcc-c++ make

3、創(chuàng)建運(yùn)行用戶、組(Nginx 服務(wù)程序默認(rèn)以 nobody 身份運(yùn)行,建議為其創(chuàng)建專門的用戶賬號,以便更準(zhǔn)確地控制其訪問權(quán)限)

useradd -M -s /sbin/nologin nginx

4、編譯安裝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 \ #指定用戶名 --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)識別nginx的操作命令


5、檢查、啟動、重啟、停止 nginx服務(wù)

nginx -t #檢查配置文件是否配置正確 #啟動 nginx #停止 cat /usr/local/nginx/logs/nginx.pid #先查看nginx的PID號 kill -3 <PID> kill -s QUIT <PID> killall -3 nginx killall -s QUIT nginx #重載 kill -1 <PID> kill -s HUP <PID> killall -1 nginx killall -s HUP nginx #日志分隔,重新打開日志文件 kill -USR1 <PID> #平滑升級 kill -USR2 <PID>

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



方法二: vim /lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx ExecrReload=/bin/kill -s HUP $MAINPID ExecrStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.targetchmod 754 /lib/systemd/system/nginx.service systemctl start nginx.service systemctl enable nginx.service

二、認(rèn)識Nginx服務(wù)的主配置文件 nginx.conf

vim /usr/local/nginx/conf/nginx.conf

1、全局配置

#user nobody; #運(yùn)行用戶,若編譯時未指定則默認(rèn)為 nobody worker_processes 1; #工作進(jìn)程數(shù)量,可配置成服務(wù)器內(nèi)核數(shù) * 2 #error_log logs/error.log; #錯誤日志文件的位置 #pid logs/nginx.pid; #PID 文件的位置

2、I/O 事件配置

events {use epoll; #使用 epoll 模型,2.6及以上版本的系統(tǒng)內(nèi)核,建議使用epoll模型以提高性能worker_connections 4096; #每個進(jìn)程處理 4096 個連接 } #如提高每個進(jìn)程的連接數(shù)還需執(zhí)行“ulimit -n 65535”命令臨時修改本地每個進(jìn)程可以同時打開的最大文件數(shù)。 #在Linux平臺上,在進(jìn)行高并發(fā)TCP連接處理時,最高的并發(fā)數(shù)量都要受到系統(tǒng)對用戶單一進(jìn)程同時可打開文件數(shù)量的限制(這是因?yàn)橄到y(tǒng)為每個TCP連接都要創(chuàng)建一個socket句柄,每個socket句柄同時也是一個文件句柄)。 #可使用ulimit -a命令查看系統(tǒng)允許當(dāng)前用戶進(jìn)程打開的文件數(shù)限制.

3、HTTP 配置

http {##文件擴(kuò)展名與文件類型映射表include mime.types;##默認(rèn)文件類型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"';##訪問日志位置#access_log logs/access.log main;##支持文件發(fā)送(下載)sendfile on;##此選項(xiàng)允許或禁止使用socke的TCP_CORK的選項(xiàng)(發(fā)送數(shù)據(jù)包前先緩存數(shù)據(jù)),此選項(xiàng)僅在使用sendfile的時候使用#tcp_nopush on;##連接保持超時時間,單位是秒#keepalive_timeout 0;keepalive_timeout 65;##gzip模塊設(shè)置,設(shè)置是否開啟gzip壓縮輸出#gzip on;

Web 服務(wù)的監(jiān)聽配置

server {##監(jiān)聽地址及端口listen 80; ##站點(diǎn)域名,可以有多個,用空格隔開server_name www.zhangsan.com;##網(wǎng)頁的默認(rèn)字符集charset utf-8;##根目錄配置location / {##網(wǎng)站根目錄的位置/usr/local/nginx/htmlroot html;##默認(rèn)首頁文件名index index.html index.php;}##內(nèi)部錯誤的反饋頁面error_page 500 502 503 504 /50x.html;##錯誤頁面配置location = /50x.html {root html;}} }

4、日志格式設(shè)定

$remote_addr與$http_x_forwarded_for用以記錄客戶端的ip地址; $remote_user:用來記錄客戶端用戶名稱; $time_local: 用來記錄訪問時間與時區(qū); $request: 用來記錄請求的url與http協(xié)議; $status: 用來記錄請求狀態(tài);成功是200, $body_bytes_sent :記錄發(fā)送給客戶端文件主體內(nèi)容大小; $http_referer:用來記錄從那個頁面鏈接訪問過來的; $http_user_agent:記錄客戶瀏覽器的相關(guān)信息; 通常web服務(wù)器放在反向代理的后面,這樣就不能獲取到客戶的IP地址了,通過$remote_add拿到的IP地址是反向代理服務(wù)器的iP地址。反向代理服務(wù)器在轉(zhuǎn)發(fā)請求的http頭信息中,可以增加x_forwarded_for信息,用以記錄原有客戶端的IP地址和原來客戶端的請求的服務(wù)器地址。location常見配置指令,root、alias、proxy_pass root(根路徑配置):請求www.kgc.com/test/1.jpg,會返回文件/usr/local/nginx/html/test/1.jpg alias(別名配置):請求www.kgc.com/test/1.jpg,會返回文件/usr/local/nginx/html/1.jpg proxy_pass(反向代理配置): proxy_pass http://127.0.0.1:8080/; 會轉(zhuǎn)發(fā)請求到http://127.0.0.1:8080/1.jpg proxy_pass http://127.0.0.1:8080; 會轉(zhuǎn)發(fā)請求到http://127.0.0.1:8080/test/1.jpg

四、訪問狀態(tài)統(tǒng)計(jì)配置

1、第一步

先使用命令/usr/local/nginx/sbin/nginx -V 查看已安裝的 Nginx 是否包含 HTTP_STUB_STATUS 模塊

2、修改 nginx.conf 配置文件,指定訪問位置并添加 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 { #訪問位置為/statusstub_status on; #打開狀態(tài)統(tǒng)計(jì)功能access_log off; #關(guān)閉此位置的日志記錄}} }

3、重啟服務(wù),訪問測試

systemctl restart nginx瀏覽器訪問 http://192.168.172.20/status Active connections :表示當(dāng)前的活動連接數(shù); server accepts handled requests :表示已經(jīng)處理的連接信息,三個數(shù)字依次表示已處理的連接數(shù)、成功的TCP握手次數(shù)、 已處理的請求數(shù)。

五、基于授權(quán)的訪問控制

1、生成用戶密碼認(rèn)證文件

yum install -y httpd-tools htpasswd -c /usr/local/nginx/passwd.db zhangsan chmod 400 /usr/local/nginx/passwd.db


2、修改主配置文件相對應(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ù),訪問測試

nginx -t systemctl restart nginx瀏覽器訪問 http://192.168.172.20



六、基于客戶端的訪問控制

訪問控制規(guī)則如下: deny IP/IP 段:拒絕某個 IPIP 段的客戶端訪問。 allow IP/IP 段:允許某個 IPIP 段的客戶端訪問。 規(guī)則從上往下執(zhí)行,如匹配則停止,不再往下匹配。vim /usr/local/nginx/conf/nginx.conf ......server {location / {......##添加控制規(guī)則##deny 192.168.172.20; #拒絕訪問的客戶端 IPallow all; #允許其它IP客戶端訪問}}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)頁文檔

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.html

3、修改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ù),訪問測試

systemctl restart nginx瀏覽器訪問 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)聽地址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)聽地址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瀏覽器訪問 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)聽 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)聽 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瀏覽器訪問 http://192.168.172.20:8080 http://192.168.172.20:8888


超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生

總結(jié)

以上是生活随笔為你收集整理的Web服务-Nginx网页服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。