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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

学习Nging笔记

發(fā)布時(shí)間:2024/1/18 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 学习Nging笔记 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. Nginx負(fù)載均衡配置

worker_processes 1;events {worker_connections 1024; } http {include mime.types;default_type application/octet-stream;sendfile on;#httpds命名要和proxy_pass代理的名一樣,下面就就是輪詢兩臺(tái)nginx服務(wù)器upstream httpds{server 192.168.3.11:80;server 192.168.3.12:80;}server {listen 80;server_name localhost;location / {proxy_pass http://httpds;root html;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}} }

2. 輪詢權(quán)重配置

worker_processes 1;events {worker_connections 1024; }http {include mime.types;default_type application/octet-stream;sendfile on;# httpds命名要和proxy_pass代理的名一樣,下面就就是輪詢兩臺(tái)nginx服務(wù)器# weight 代表權(quán)重# down 代表下線# backup備用機(jī),其他機(jī)器都下線或者宕機(jī)了,就用備用機(jī)upstream httpds{server 192.168.3.11:80 weight=8 down;server 192.168.3.12:80 weight=2 down;server 192.168.3.13:80 weight=1 backup;}server {listen 80;server_name localhost;location / {proxy_pass http://httpds;root html;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}} }

3. nginx動(dòng)靜分離(把一些靜態(tài)資源放在nginx,而不是在后臺(tái)服務(wù)中)

在nginx.conf中配置多個(gè)location放在server塊中,把靜態(tài)資源放在html目錄下

# css配置location /css {root html;index index.html index.htm;}#js配置location /js {root html;index index.html index.htm;}# 圖片配置location /img {root html;index index.html index.htm;}

合并配置(正則匹配)

location ~*/(js|img|css) {root html;index index.html index.htm;}

4. URLrewrite 偽靜態(tài)配置(隱藏真實(shí)地址)

location / {#^/2.html是要訪問(wèn)的地址 # /index.jsp?pageNum=2 真實(shí)訪問(wèn)地址rewrite ^/2.html /index.jsp?pageNum=2 break;proxy_pass http://192.168.3.13;}

正則匹配

location / {# 這樣就不用和上面一樣寫死rewrite ^/([0-9]+).html$ /index.jsp?pageNum=&1 break;proxy_pass http://192.168.3.13;}

5. 開(kāi)啟防火墻,只能由指定的服務(wù)器訪問(wèn)

開(kāi)啟對(duì)應(yīng)的防火墻(不對(duì)外開(kāi)放的)

systemctl start firewalld

添加指定端口和ip訪問(wèn)(添加之后記得重新啟動(dòng)防火墻)

firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.3.10" port protocol="tcp" port="80" accept"

移除規(guī)則

firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.3.10" port protocol="tcp" port="80" accept"

重啟防火墻

firewall-cmd --reload

查看已配置規(guī)則

firewall-cmd --list-all

6. 防盜鏈

下面防止圖片被盜

location ^~/images/ {valid_referers 192.168.3.10; #valid_referers 指令,配置是否允許 referer 頭部以及允許哪些 referer 訪問(wèn)。 192.168.3.10 不是ip而是域名(去掉http:// 前綴)if ($invalid_referer) { # 注意這里if后要加空格return 403; ## 返回錯(cuò)誤碼}root /www/resources;}
  • 參數(shù)值

  • none:允許沒(méi)有 referer 信息的請(qǐng)求訪問(wèn),即直接通過(guò)url訪問(wèn)。

  • blocked:請(qǐng)求頭Referer字段不為空(即存在Referer),但是值可以為空(值被代理或者防火墻刪除了),并且允許refer不以“http://”或“https://”開(kāi)頭,通俗點(diǎn)說(shuō)就是允許“http://”或"https//"以外的請(qǐng)求。

  • server_names:若 referer 中站點(diǎn)域名與 server_name 中本機(jī)域名某個(gè)匹配,則允許該請(qǐng)求訪問(wèn)

  • 其他字符串類型:檢測(cè)referer與字符串是否匹配,如果匹配則允許訪問(wèn),可以采用通配符*

  • 正則表達(dá)式:若 referer 的值匹配上了正則,就允許訪問(wèn)

  • 7. 利用curl測(cè)試防盜鏈

    下載curl

    yum install -y curl

    測(cè)試curl 192.168.3.10

    沒(méi)有referer詳情下

    curl -I http://192.168.3.10/img/2.jpg


    有referer詳情下

    # -e 后面跟著就是referer的地址 curl -e "http://baidu.com" -I http://192.168.3.10/img/2.jpg

    8. 防盜鏈資源返回頁(yè)面錯(cuò)誤提示和圖片提示

    新建一個(gè)錯(cuò)誤提示頁(yè)面

    location ~*/(js|img|css) {valid_referers 192.168.3.10;if ($invalid_referer) { # 需要調(diào)到的錯(cuò)誤頁(yè)面return 401; }root html;index index.html index.htm;}# 錯(cuò)誤頁(yè)面配置error_page 401 /401.html;location = /401.html {root html;}

    圖片提示

    valid_referers 192.168.3.10;if ($invalid_referer) { # rewrite 到需要提示的圖片上面rewrite ^/ /img/3.jpg break;# return 401; }

    9. nginx高可用及Keepalived實(shí)戰(zhàn)

  • 安裝命令
  • yum install -y keepalived
  • 修改配置/etc/keepalived/keepalived.conf
  • ! Configuration File for keepalivedglobal_defs {router_id LB_10 }vrrp_instance VI_102 {state MASTERinterface ens33virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.3.200} } ! Configuration File for keepalivedglobal_defs {router_id LB_14 }vrrp_instance VI_102 {state BACKUPinterface ens33virtual_router_id 51priority 50advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.3.200} }
  • 啟動(dòng)keepalived
  • systemctl start keepalived
  • 可以ping通

  • 把10關(guān)閉

  • 還是可以ping通

  • 通過(guò)頁(yè)面訪問(wèn)

  • 10. nginx線上實(shí)戰(zhàn)

  • 前面需要買域名和買服務(wù)器

  • 申請(qǐng)證書





  • 3. 申請(qǐng)完就可以下載對(duì)應(yīng)的證書了

    4. 上傳到nginx

  • nginx配置證書(在nginx.conf里面配置):
  • server {listen 443 ssl;server_name localhost; # 接收所有訪問(wèn)443端口的請(qǐng)求ssl_certificate 9028382_upczt.com.pem;ssl_certificate_key 9028382_upczt.com.key;index index.html index.htm index.php;#error_page 404 /404.html;#error_page 502 /502.html;location /nginx_status {stub_status on;access_log off;allow 127.0.0.1;deny all;}location ~ [^/]\.php(/|$) {#fastcgi_pass remote_php_ip:9000;fastcgi_pass unix:/dev/shm/php-cgi.sock;fastcgi_index index.php;include fastcgi.conf;}location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {expires 30d;access_log off;}location ~ .*\.(js|css)?$ {expires 7d;access_log off;}location ~ ^/(\.user.ini|\.ht|\.git|\.svn|\.project|LICENSE|README.md) {deny all;}location /.well-known {allow all;} }

    server {
    listen 80;
    server_name upczt.com www.upczt.com; #修改成自己的域名
    access_log /data/wwwlogs/access_nginx.log combined;
    return 301 https:// s e r v e r n a m e server_name servern?amerequest_uri;
    root html;

    }


    6. 保存配置文件重啟nginx

    systemctl reload nginx

    7. 加上https:// 變?yōu)榘踩珔f(xié)議了
    8. 安裝discuz.net

    9. 上傳到服務(wù)器

    10. 解壓

    11. 修改名字為bbs

    12. Nginx配置springboot項(xiàng)目使用httos協(xié)議

  • springboot 項(xiàng)目配置文件
  • server.tomcat.remote_ip_header=x-forwarded-for server.tomcat.protocol_header=x-forwarded-proto server.tomcat.port-header=X-Forwarded-Port
  • 打包上傳到服務(wù)器,簡(jiǎn)單寫了個(gè)接口


  • 3. 配置服務(wù)器的安全組端口8080

  • nginx配置文件
  • server {listen 80;server_name upczt.com www.upczt.com;access_log /data/wwwlogs/access_nginx.log combined;return 301 https://$server_name$request_uri; root html;}server {listen 443;server_name www.upczt.com;ssl on;#配置證書的路徑ssl_certificate 9028382_upczt.com.pem;ssl_certificate_key 9028382_upczt.com.key;location / {#配置轉(zhuǎn)發(fā)到8080端口proxy_pass http://www.upczt.com:8080;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_set_header X-Forwarded-Port $server_port;}}
  • 重啟nginx
  • 總結(jié)

    以上是生活随笔為你收集整理的学习Nging笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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