nginx實戰操作(常用命令及配置)


1. nginx介紹

2. nginx常用命令

	驗證配置是否正確: nginx  -t
  查看Nginx的詳細的版本號:nginx  -V
  查看Nginx的簡潔版本號:nginx  -v
  啟動Nginx:start  nginx
	快速停止或關閉Nginx:nginx   -s   stop
	正常停止或關閉Nginx:nginx   -s   quit
	配置文件修改重裝載命令:nginx   -s  reload

3. nginx配置

3.1 配置代碼

#user  nobody;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

 #1 start
	upstream linuxidc {
			server localhost:7071;
			server localhost:7072;
			server localhost:7073;
	}
   server {
       listen      7070;
       server_name  localhost;
       location / {
          # root    C:/ngtest2;
         # index  index.html index.htm;
         proxy_pass http://linuxidc;
        }
    }
# 1 end
 #2 start
   server {
       listen      7071;
       server_name  localhost;
       location / {
           root    C:/ngtest1;
         # index  index.html index.htm;
         #proxy_pass https://tms;
         #proxy_pass https://www.baidu.com/;
        }
    }
   server {
       listen      7072;
       server_name  localhost;
       location / {
           root    C:/ngtest2;
         # index  index.html index.htm;
         #proxy_pass https://tms;
        }
    }
   server {
       listen      7073;
       server_name  localhost;
       location / {
          root    C:/ngtest3;
         # index  index.html index.htm;
         #proxy_pass https://tms;
        }
    }

# 2 end
 #3 start
    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        
        #location / {
          #  root   C:\ngtest;
            #index  index.html index.htm;
            #proxy_pass https://www.baidu.com/;
       # }

        location /baidu {
            #root   html;
            #index  index.html index.htm;
            proxy_pass https://www.baidu.com/;
        }
        location /csdn {
            #root   html;
            #index  index.html index.htm;
            proxy_pass https://www.csdn.net/;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # 3 end
    }
}

3.2 代碼說明及效果圖

3.2.1 不同端口代理不同應用
#2 start
   server {
       listen      7071;
       server_name  localhost;
       location / {
           root    C:/ngtest1;
         # index  index.html index.htm;
         #proxy_pass https://tms;
         #proxy_pass https://www.baidu.com/;
        }
    }
   server {
       listen      7072;
       server_name  localhost;
       location / {
           root    C:/ngtest2;
         # index  index.html index.htm;
         #proxy_pass https://tms;
        }
    }
   server {
       listen      7073;
       server_name  localhost;
       location / {
          root    C:/ngtest3;
         # index  index.html index.htm;
         #proxy_pass https://tms;
        }
    }

# 2 end
3.2.2 效果圖

3.2.3 同一端口號代理不同應用
 #3 start
    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        
        #location / {
          #  root   C:\ngtest;
            #index  index.html index.htm;
            #proxy_pass https://www.baidu.com/;
       # }

        location /baidu {
            #root   html;
            #index  index.html index.htm;
            proxy_pass https://www.baidu.com/;
        }
        location /csdn {
            #root   html;
            #index  index.html index.htm;
            proxy_pass https://www.csdn.net/;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # 3 end
3.2.4 效果圖

3.2.5 負載均衡

默認采用的輪詢方式

#1 start
	upstream linuxidc {
			server localhost:7071;
			server localhost:7072;
			server localhost:7073;
	}
   server {
       listen      7070;
       server_name  localhost;
       location / {
          # root    C:/ngtest2;
         # index  index.html index.htm;
         proxy_pass http://linuxidc;
        }
    }
# 1 end
3.2.6 效果圖



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM