鍍金池/ 問答/C  Linux  網(wǎng)絡(luò)安全/ nginx是如何實現(xiàn)端口復(fù)用的??

nginx是如何實現(xiàn)端口復(fù)用的??

RT
一份nginx實例通常由多進程構(gòu)成
那么nginx是如何實現(xiàn)端口復(fù)用的?

端口復(fù)用的技術(shù)知道一些,但不知nginx采用的是哪一種

回答
編輯回答
朽鹿

nginx1.9之后,通過SO_REUSEPORT支持端口服用,該socket參數(shù)由操作系統(tǒng)提供,允許多個套接字偵聽相同的IP地址和端口組合,內(nèi)核負責(zé)調(diào)度。
https://www.nginx.com/blog/so...

2018年6月8日 09:58
編輯回答
淚染裳

提供一個思路供參考,這也是我的個人服務(wù)端器nginx的配置:

worker_processes  2;

error_log  /mnt/logs/nginx/error.log error;

events {
    use   epoll;
    multi_accept on;
    worker_connections  5120;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    fastcgi_intercept_errors on;

    client_max_body_size 6M;
    client_body_buffer_size 1M;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    
    server {
        listen       80;
        server_name  www.sosout.com;
        root /mnt/html;
        index index.jsp index.htm index.html;
        location ~ ^/favicon\.ico$ {
            root /mnt/html;
        }
        if ($host ~* "^(.*?)\.sosout\.com$") {
            set $domain $1;
        }
        location /blog {
            if ($domain = "blog.sosout.com") {
                return 404; #防止有人訪問www.sosout.com/blog 看到blog二級域名的頁面,只允許訪問blog.sosout.com查看
            }
        }
        location /admin {
            if ($domain = "admin.sosout.com") {
                return 404; #防止有人訪問www.sosout.com/admin 看到admin二級域名的頁面,只允許訪問admin.sosout.com查看
            }
        }
            
        error_page  404              /404.html;
    }

    server {
        listen 80;
        server_name  *.sosout.com;
        
        if ($host ~* "^(.*?)\.sosout\.com$") {
            set $domain $1;
        }

        location / {
            if ($domain ~* "blog") {
                root /mnt/html/blog;
            }
            if ($domain ~* "admin") {
                root /mnt/html/admin;
            }
            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_set_header   X-Forwarded-Proto  $scheme;
        }
        access_log  /mnt/logs/nginx/access.log  main;
    }
    include /etc/nginx/conf.d/*.conf;
}
2018年7月28日 09:26
編輯回答
貓館

那些多進程都是 fork 出來的,父進程 fork 出來的子進程是可以繼承使用父進程的 socket 的。

2017年8月23日 07:44