A Brief Introduction to nginx

What is Nginx

Nginx descriptionNginx can be used as a web server, but more often, we use it as a gateway because it has the necessary functions of a gateway:

  • Direction agent
  • Load balancing
  • Dynamic routing
  • Request filtering

Server type

Web servers are divided into 2 categories:

  1. Web application server:
    • tomcat
    • resin
    • jetty
  2. Web server
    • Apache
    • Nginx
    • IIS

Distinction: Web servers cannot parse pages such as JSP, but can only process static resources such as JS/CSS/html.
Concurrency: The concurrency capability of the Web server is much higher than that of the Web application server.

Nginx direction proxy

What is a direction agency?
  • Proxy: Through the configuration of the client, a server can act as a proxy for the client, and all requests from the client are handed over to the proxy server for processing.
  • Reverse proxy: Use a server to proxy the real server. When the user visits, the real server is no longer accessed, but the proxy server.

Nginx can be used as a direction proxy server:

  • We need to configure reverse proxy rules in nginx in advance, and different requests are handed over to different real servers for processing
  • When the request arrives in nginx, nginx will forward the request according to the defined rules to realize the routing function

nginx directory structure
nginx directory structure diagram1. conf: configuration directory
2. contrib: third-party dependencies
3. html: default static resource directory, similar to tomcat's webapps
4. logs: log directory
5. nginx.exe: startup program. You can double-click to run, but this is not recommended.

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
	server{
		listen 80;
		server_name manage.ishop.com;
		
		proxy_set_header X-Forworded-Host $host;
		proxy_set_header X-Forworded-Server $host;
		proxy_set_header X-Forworded-For $proxy_add_x_forwarded_for;
		
		location / {
			proxy_pass http://127.0.0.1:9001;
			proxy_connect_timeout 600;
			proxy_read_timeout 600;
		}
	}
	server{
		listen 80;
		server_name api.ishop.com;
		
		proxy_set_header X-Forworded-Host $host;
		proxy_set_header X-Forworded-Server $host;
		proxy_set_header X-Forworded-For $proxy_add_x_forwarded_for;
		
		location / {
			proxy_pass http://127.0.0.1:9002;
			proxy_connect_timeout 600;
			proxy_read_timeout 600;
		}
	}
   #server {
   #       listen       80;
   #	server_name localhost;

   #     #charset koi8-r;

   #     #access_log  logs/host.access.log  main;

   #    location / {
   #        root   html;//定义网站根目录,目录可以是相对路径也可以是绝对路径。
   #        index  index.html index.htm;//定义站点的默认页。
   #    }

   #     #error_page  404              /404.html;

   #    # redirect server error pages to the static page /50x.html
   #    #
   #     error_page   500 502 503 504  /50x.html;
   #     location = /50x.html {
   #        root   html;//定义50x.html所在路径
   #     }

   #   # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
   #    #location ~ \.php$ {
   #    #    proxy_pass   http://127.0.0.1;
   #    #}

   #   # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
   #   #
   #   #location ~ \.php$ {
   #   #    root           html;
   #    #    fastcgi_pass   127.0.0.1:9000;
   #    #    fastcgi_index  index.php;
   #    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
   #   #    include        fastcgi_params;
   #    #}

   #    # deny access to .htaccess files, if Apache's document root
   #    # concurs with nginx's one
   #   #
   #    #location ~ /\.ht {
   #    #    deny  all;
   #    #}
   # }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

Insert picture description here
nginx command
start nginx start nginx.exe
restart nginx -s reload
stop nginx -s stop

SwitchHosts-win32-x64_v3.3.11.5347

SwitchHosts is an open source tool for modifying, managing, and switching multiple hosts schemes.
Insert picture description hereInsert picture description hereIn addition to helping you quickly switch between different hosts settings and edit hosts files, SwitchHosts also has some very good features, such as:

免费、开源、支持三大操作系统
系统托盘快速「一键切换」不同的 hosts 方案
支持 hosts 文件语法高亮,可以方便用户更直观地阅读和修改 Hosts 内容;
编辑 hosts 时,可以点击行号可以对行进行快速注释或取消注释
支持远程 hosts,直接从指定网址读取 hosts 内容,方便多台机器同步 hosts 设置,这是一个很赞的特性!
支持 hosts 配置的导入、导出备份
macOS 系统下可以支持 Alfred workflow 快速切换

Basically, with SwitchHosts, you can do whatever you want with the hosts and switch easily with one click. Moreover, the remote hsots solution can also be very convenient for people who often change computers and use it everywhere! This is also its highlight

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_42789301/article/details/105406042