nginx web服務器概念了解 配置

服務器

服務器

服務器是一種提供高效計算的機器,與普通的PC主機相比,具有可觀的穩定性,高併發性,可擴展性。

互聯網任何一個應用都是以服務器為基礎設施的,沒有服務器我們就無法訪問網絡上的任何內容,只能使用單機的應用。例如網站,我們訪問的任何一個網站都是保存在某個服務器上的,域名被DNS(域名解析服務器)解析到IP地址后,瀏覽器就能通過IP地址訪問對應的服務器資源了。

就好比:服務器是人的家,人名相當於域名(不可重名),身份證號相當於IP地址。通過人名搜索到身份證號,通過身份證號獲取到家的地址。

Web服務器

Web服務器不再是一種硬件設施,而是一種部署在服務器上的軟件應用,它服務於各種網絡請求,將網絡請求進行處理,分發。

所以Web服務器的處理能力很大程度決定了該網站的併發能力。著名的Web服務器有:Apache Nginx

Web應用服務器

Web應用服務器是專門處理邏輯代碼的服務器,同時還具有了處理網絡請求的能力,一般為了提高併發能力,會在Web應用服務器上套一層Web服務器。

例如:Tomcat uwsgi gunicorn,后兩個是Python的Web應用服務器,專門處理Python的邏輯代碼。

聯繫

其中Web服務器和Web應用服務器都部署在服務器上。

Nginx服務器

Nginx (engine x) 是一個高性能的HTTP和反向代理web服務器,同時也提供了IMAP/POP3/SMTP服務。其主要特點如下:

  • 輕量級 併發能力強
  • 支持處理靜態資源,以減少應用服務器的壓力
  • 負載均衡

負載均衡

大型的網站應用網站應用往往是由無數個服務器服務的,就像淘寶這種,單靠一個是不可能承受的了如此大的併發量,因此有了負載均衡。負載均衡又分為硬負載均衡軟負載均衡,硬負載均衡是通過硬件的方式實現負載均衡,比如F5,成本都比較昂貴,軟負載均衡則是通過軟件的方式實現,比如Nginx和Apache。

所謂負載均衡就是將多個請求分發到不同的服務器上去,每個服務器都有處理請求和邏輯的應用服務器,以此來提高併發量。

下面使用Nginx來實現負載均衡配置

配置Nginx需要到/etc/nginx/nginx.conf文件內進行編輯

http {
##http的配置

server {
        listen 80;//監聽端口
        server_name 域名;
        location / {
            proxy_pass http://lca;
        }
    }
 upstream lca {//採用輪詢方式,依次將請求轉發到各個服務器
        server  192.168.1.1:5000;
        server  192.168.1.2:5000;
        server  192.168.1.3:5000;
    }
}

上面是採用輪詢的方式實現端口轉發 負載均衡,還有幾種方式實現:

  • 權重方式:指定每個服務的權重比例,weight和訪問比率成正比,通常用於後端服務機器性能不統一,將性能好的分配權重高來發揮服務器最大性能.
 upstream lca {
        server  192.168.1.1:5000 weight=1;
        server  192.168.1.2:5000 weight=2;
        server  192.168.1.3:5000 weight=3;
    }
  • iphash
    每個請求都根據訪問ip的hash結果分配,經過這樣的處理,每個用戶固定訪問一個後端服務。
 upstream lca {//權重與iphash結合
        ip_hash
        server  192.168.1.1:5000 weight=1;
        server  192.168.1.2:5000 weight=2;
        server  192.168.1.3:5000 weight=3;
    }

解決跨域問題

跨域請求問題

為了提高瀏覽器的安全性,引入了跨域限制,也就是同源策略。

所謂源:如果兩個頁面(接口)的協議,端口或者域名都相同,那麼兩個頁面就有相同的源。如果在同一個頁面訪問不同源的資源,則會出現500錯誤。

  • 瀏覽器從一個域名的網頁去請求另一個域名的資源時,域名、端口、協議任一不同,都是跨域
  • 跨域限制主要是為了安全考慮

前端在請求後端資源時,往往會出現錯誤代碼500的情況。

nginx解決跨域問題

解決跨域問題的方式有很多,在這裏介紹通過nginx來解決跨域問題。

上面說到客戶端瀏覽器同時請求服務器的不同資源若不是同源就會引發同源策略問題,那要是服務器請求服務器呢?那麼照這個思路就想到了nginx的反向代理。
我們可以使用nginx的反向代理,將不同源的服務器集中到一個nginx web服務器下,也就是通過nginx來反向代理各個服務器。

server
{
 listen 80;
 server_name cola666.top;
// =/ 表示精確匹配路徑為/的url,真實訪問為http://localhost:8000
 location = / {
 proxy_pass http://localhost:8000;
 }
//當匹配到/a的url自動去localhost:8000
location /a{
 proxy_pass http://localhost:8001;
 }
 location /baidu/ {
 proxy_pass http://www.baidu.com/;
 }
}
  • 當有請求www.cola666.top的資源時,服務器接收到,會自動將請求內容交給localhost:8000web服務器處理。
  • 當有請求www.cola666.top/a下的資源時,服務器接收到,會自動將請求內容交給localhost:8001web服務器處理。
  • 當有請求www.cola666.top/baidu/下的資源時,服務器接收到,會請求百度的服務器資源。

雖然請求同源url,但實際上nginx幫助我們轉發到其他web服務器,所以實際上訪問的是非同源url資源,以實現跨域問題

location匹配規則
  • 當一個url匹配到多個location時,nginx將請求轉發給匹配最長的location來處理
  • 代理
 location /b/ {
 proxy_pass http://www.baidu.com/;
 }

 location /b/ {
 proxy_pass http://www.baidu.com;
 }

二者的區別為後者會將location中的/b/也添加進url中,比如,後者則代理到http://www.baidu.com/b/xxx,前者則是http:///www.baidu.com/xxx

下面為一個比較簡易的完整的nginx配置

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65; 
    server {
        listen       80;
        location / {
         proxy_pass http://localhost:8080;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

最後附加一個全面的nginx配置,包括靜態資源緩存,負載均衡,Https,等等

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
		#自定義的日誌格式
		log_format  main  '[($remote_addr) - ($remote_user [$time_local]) $request" '
		'($status) '
		'($http_user_agent)($http_x_forwarded_for)'
		'($upstream_addr) ($upstream_response_time) ($request_time) ]';

		proxy_cache_path /data/nginx/tmp-test levels=1:2 keys_zone=tmp-test:100m inactive=7d max_size=10g;

		access_log  /var/log/nginx/access.log  main;

		gzip  on;
		gzip_min_length 1k;
		gzip_buffers    16 64k;
		gzip_http_version 1.1;
		gzip_comp_level 4;
		gzip_types  text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
		gzip_vary on;
		sendfile            on;
		tcp_nopush          on;
		tcp_nodelay         on;
		keepalive_timeout   65;
		types_hash_max_size 2048;
		include             /etc/nginx/mime.types;
		default_type        application/octet-stream;

		include /etc/nginx/conf.d/*.conf;

		#allow to access by the same origin
		add_header 'Access-Control-Allow-Origin' '*';
		add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
		add_header 'Access-Control-Allow-Credentials' 'true';

	upstream zzm {
		server localhost:8000;
		#server locahost:8001;
	}

	server {
		listen       80 default_server;
		listen       [::]:80 default_server;
		server_name  _;
		root         /usr/share/nginx/html;

		# Load configuration files for the default server block.
		include /etc/nginx/default.d/*.conf;

		location /static{
			alias /var/static;
		}

		location /{
			proxy_cache tmp-test;
			proxy_cache_key $uri;
			add_header Access-Control-Allow-Origin *;
			add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
			add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
			#include uwsgi_params;
			proxy_pass http://zzm;

			# if django be used socket model to start up,using uwsgi of the following
			#uwsgi_pass 127.0.0.1:8000;
			#uwsgi_read_timeout 180;

			proxy_redirect off;
			proxy_set_header        Host    $host;
			proxy_set_header        REMOTE_ADDR     $remote_addr;
			proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

			real_ip_recursive on;
		}

		error_page 404 /404.html;
			location = /40x.html {
		}

		error_page 500 502 503 504 /50x.html;

		location = /50x.html {

		}
	}


	#配置https
	server {
		listen       443 ssl http2 default_server;
		listen       [::]:443 ssl http2 default_server;
		server_name  cola666.top;
		root         /usr/share/nginx/html;

		ssl_certificate  /var/xxx.pem;#ssl兩個證書路徑
		ssl_certificate_key /var/xxx.key;
		ssl_session_cache shared:SSL:1m;
		ssl_session_timeout  10m;
		ssl_ciphers HIGH:!aNULL:!MD5;
		ssl_prefer_server_ciphers on;

		# Load configuration files for the default server block.
		include /etc/nginx/default.d/*.conf;

		#靜態資源路徑
		location /static{
			alias /var/static;
		}

		location / {
			#緩存路徑
			proxy_cache tmp-test;
			proxy_cache_key $uri;

			add_header Access-Control-Allow-Origin *;
			add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
			add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
			#include uwsgi_params;
			#uwsgi_pass 127.0.0.1:8000;
			proxy_pass http://zzm;
			proxy_redirect off;
			#將客戶端ip地址交給服務器後端
			proxy_set_header        Host    $host;
			proxy_set_header        REMOTE_ADDR     $remote_addr;
			proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
			real_ip_recursive on;
		}

		error_page 404 /404.html;
			location = /40x.html {
		}
		error_page 500 502 503 504 /50x.html;
			location = /50x.html {
		}
	}
}
```![](https://img2020.cnblogs.com/blog/1624549/202006/1624549-20200628205157078-48009301.png)

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

【其他文章推薦】

USB CONNECTOR掌控什麼技術要點? 帶您認識其相關發展及效能

※評比前十大台北網頁設計台北網站設計公司知名案例作品心得分享

※智慧手機時代的來臨,RWD網頁設計已成為網頁設計推薦首選

※評比南投搬家公司費用收費行情懶人包大公開

※幫你省時又省力,新北清潔一流服務好口碑