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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

nginx实现http服务配置

發布時間:2024/9/30 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nginx实现http服务配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

靜態文件

即實現nginx代理指向靜態文件,動靜分離
主要配置如下:

worker_processes 1;events {worker_connections 1024; }http {include mime.types;default_type application/octet-stream;server {listen 80;server_name localhost;location /images {root /Users/jun/data;}location / {root /Users/jun/data/www;index index.html index.htm;}}include servers/*; }

http://localhost/images/jie.png的請求會匹配上面的location,返回本地/Users/jun/data/images/jie.png的圖片,
http://localhost/ 的請求會匹配到下面的location,然后返回本地/Users/jun/data/www/下面的index.html文件。

開始一直搞不懂文件匹配規則,其實就是匹配到相應的location后,把請求url拼接到root的參數后去本地文件系統中查找,如剛才http://localhost/請求,返回/Users/jun/data + /即/Users/jun/data/下的index.html(缺省文件),對于http://localhost/images/jie.png請求 就返回/Users/jun/data/www + /images/jie.png 即服務器本地/Users/jun/data/images/jie.png的圖片。

至于匹配哪個location,當符合多個locating的匹配規則時,選取最長的那個,如上面http://localhost/images/jie.png的請求其實兩個location規則都符合,但nginx會選取前綴最長的那個/images。再比如http://localhost/some/example.png請求就會去尋找/Users/jun/data/www/some/example.png

負載均衡的配置

如果應用程序以集群方式部署,我們需要采取負載均衡,來看nginx如何實現:

http {#設定mime類型,類型由mime.type文件定義include /etc/nginx/mime.types;default_type application/octet-stream;#設定日志格式access_log /var/log/nginx/access.log;#設定負載均衡的服務器列表upstream load_balance_server {#weigth參數表示權值,權值越高被分配到的幾率越大server 192.168.1.11:80 weight=5;server 192.168.1.12:80 weight=1;server 192.168.1.13:80 weight=6;}#HTTP服務器server {#偵聽80端口listen 80;#定義使用www.xx.com訪問server_name www.helloworld.com;#對所有請求進行負載均衡請求location / {root /root; #定義服務器的默認網站根目錄位置index index.html index.htm; #定義首頁索引文件的名稱proxy_pass http://load_balance_server ;#請求轉向load_balance_server 定義的服務器列表#以下是一些反向代理的配置(可選擇性配置)#proxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;#后端的Web服務器可以通過X-Forwarded-For獲取用戶真實IPproxy_set_header X-Forwarded-For $remote_addr;proxy_connect_timeout 90; #nginx跟后端服務器連接超時時間(代理連接超時)proxy_send_timeout 90; #后端服務器數據回傳時間(代理發送超時)proxy_read_timeout 90; #連接成功后,后端服務器響應時間(代理接收超時)proxy_buffer_size 4k; #設置代理服務器(nginx)保存用戶頭信息的緩沖區大小proxy_buffers 4 32k; #proxy_buffers緩沖區,網頁平均在32k以下的話,這樣設置proxy_busy_buffers_size 64k; #高負荷下緩沖大小(proxy_buffers*2)proxy_temp_file_write_size 64k; #設定緩存文件夾大小,大于這個值,將從upstream服務器傳client_max_body_size 10m; #允許客戶端請求的最大單文件字節數client_body_buffer_size 128k; #緩沖區代理緩沖用戶端請求的最大字節數}} }

2.3 https反向代理配置

現在越來越多的網站采用https協議來提高應用的安全性,所以我們需要知道nginx里面https如何配置。
首先要注意:HTTPS 的固定端口號是 443,不同于 HTTP 的 80 端口SSL 標準需要引入安全證書,所以在 nginx.conf 中你需要指定證書和它對應的 key。

upstream tomcat{ip_hash;server 127.0.0.1:9080;}#控制全局nginx所有請求報文大小client_max_body_size 2m;server {listen 80;server_name 127.0.0.1;#charset koi8-r;#access_log logs/host.access.log main;location /web/database/back/restoreDataBaseByFileName {rewrite ^ https://$server_name:443$request_uri?permanent;}location / {rewrite ^ https://$server_name:443$request_uri?permanent;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}server {listen 443 ssl;server_name 127.0.0.1;ssl_certificate ..\\ca\\server\\server.cer;ssl_certificate_key ..\\ca\\server\\server.key;ssl_protocols TLSv1.2;ssl_ciphers 'XXXX-RSA-AES128-GCM-SHA256:-ECDSA-AES256-SHA:-RSA-AES128-SHA256:-RSA-AES128-SHA:DHE-DSS-DSS-AES256-SHA:-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SA-XXXX';ssl_session_cache shared:SSL:1m;ssl_session_timeout 30m;charset utf-8;ssl_dhparam \\dhparams.pem;#access_log logs/host.access.log main;location /web/database/back/restoreDataBaseByFileName {root html;index index.html index.htm;proxy_http_version 1.1; chunked_transfer_encoding off;proxy_connect_timeout 6000; proxy_read_timeout 6000; proxy_send_timeout 6000; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; proxy_headers_hash_max_size 51200; proxy_headers_hash_bucket_size 6400;proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://tomcat;}location / {root html;index index.html index.htm;proxy_http_version 1.1; chunked_transfer_encoding off;proxy_connect_timeout 60; proxy_read_timeout 60; proxy_send_timeout 60; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; proxy_headers_hash_max_size 51200; proxy_headers_hash_bucket_size 6400;proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://tomcat;}#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}

2.4 多個webapp部署到同一機器不同端口

當一個網站功能越來越豐富時,往往需要將一些功能相對獨立的模塊剝離出來,獨立維護。這樣的話,通常,會有多個 webapp。

舉個例子:假如 www.helloworld.com 站點有好幾個 webapp,finance(金融)、product(產品)、admin(用戶中心)。訪問這些應用的方式通過上下文(context)來進行區分:

www.helloworld.com/finance/

www.helloworld.com/product/

www.helloworld.com/admin/

我們知道,http 的默認端口號是 80,如果在一臺服務器上同時啟動這 3 個 webapp 應用,都用 80 端口,肯定是不成的。所以,這三個應用需要分別綁定不同的端口號。

那么,問題來了,用戶在實際訪問 www.helloworld.com 站點時,訪問不同 webapp,總不會還帶著對應的端口號去訪問吧。所以,你再次需要用到反向代理來做處理。

配置也不難,來看看怎么做吧:

http {#此處省略一些基本配置upstream product_server{server www.helloworld.com:8081;}upstream admin_server{server www.helloworld.com:8082;}upstream finance_server{server www.helloworld.com:8083;}server {#此處省略一些基本配置#默認指向product的serverlocation / {proxy_pass http://product_server;}location /product/{proxy_pass http://product_server;}location /admin/ {proxy_pass http://admin_server;}location /finance/ {proxy_pass http://finance_server;}} }

轉自:靜默虛空博客

總結

以上是生活随笔為你收集整理的nginx实现http服务配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。