Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept of Nginx?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what is the concept of Nginx". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the concept of Nginx"?

1. The concept of Nginx?

"what is Nginx:"

Nginx is not only a reverse proxy server based on HTTP, but also a basic POP3/SMTP service mail server.

Reverse proxy server: now we A need to access 10.7.182.100 of the target B server. I want to access the resources on this B server. Now if I use Nginx, I can access the 10.7.182.100 server through the Nginx server.

IMAP/POP/SMTP: these three are the mail transfer protocols.

Mail server: sending mail to receive mail

Web server: itself is a Web server software, similar to Tomcat this Web service software

"what can Nginx do:"

Can be used as a Web server

Can be used as a mail server

A server that can act as a reverse proxy

Dynamic and static separation (that is, the separation of dynamic and static resources)

Load balancing can be achieved.

2. Installation of Nginx

"Nginx installation steps:"

Step 1: download our nginx here take version 1.6.2 as an example step 2: share the installation file to our linux step 3: copy the file to / usr/local below step 4: install tar-zxvf xxxx.tar.gz step 5: download the required dependent library yum install pcre yum install pcre-devel yum install zlib yum install zlib-devel step 6: configure cd nginx-1.6 for config .2 & &. / configure-- prefix=/usr/local/nginx step 7: install make & & make install step 8: start nginx/ usr/local/nginx/sbin/nginx and close:. -s stop-s reload to see if there is a problem with the port netstat-tunpl | No problem for verification by grep 80 browser

3. Parsing the configuration file of Nginx

"profile:"

# user nobody; # working thread (4 core 8 thread then the following is set to 8 physical hardware related) worker_processes 1; # where the global error log is stored # error_log logs/error.log; # the notice behind the global error log indicates the format of the output error log # error_log logs/error.log notice; # error_log logs/error.log info # where the nginx process number is stored # pid logs/nginx.pid; # the maximum number of connections (this is also related to the hardware) events {worker_connections 1024;} # the following is related to the HTTP request. Http {# represents the current server supported type include mime.types; # the default data type for transmission is stream default_type application/octet-stream # declares a log format called main log_format main'$remote_addr-$remote_user [$time_local] "$request"'$status $body_bytes_sent "$http_referer"'"$http_user_agent"$http_x_forwarded_for"' # indicates that the log record format for each request is the above main format access_log logs/access.log main; # whether you can send a file sendfile on; # tcp_nopush on; # this is a double active timeout # keepalive_timeout 0; keepalive_timeout 65; # whether to open file compression # gzip on # Virtual one host # configuration of load balancer upstream qianyu {ip_hash; server 10.7.182.110 upstream qianyu 8080; server 10.7.182.87 server 8080;} # server {# listen 90; # server_name localhost; # location / {# load # index qianyu.html #} # do a reverse proxy # means that if the suffix you visit ends with .jpg, then visit the following other server # location ~\ .jpg$ {# proxy_pass http://10.7.182.110:8080; #} # the following is to demonstrate an example of load balancing # location ~\. Jpg$ {# proxy_pass http://qianyu; #} #} # Virtual a static and dynamic separation machine server {listen 9999; server_name localhost # represents the dynamic resource access machine location / {proxy_pass http://10.7.182.54:8080;} # indicates the address of non-css and js file access location. *\. (htm | html | gif | jpg | jpeg | bmp | swf | ioc | rar | txt | flv | mid | doc | ppt | pdf | xls | mp3 | wma) ${root / usr/local/webapp; expires 30d } # indicates the access address of css and js location ~. *\. (js | css)? ${root / usr/local/webapp; expires 1h;} # Virtual server server {# port is 80 listen 80 # this means that the virtual server accesses the native server_name localhost; by default # this is the code of the virtual server # charset koi8-r; # where the logs of the current server are stored # access_log logs/host.access.log main # make an address mapping location / {root html; index index.html index.htm;} # if the page visited when reporting 404 is # error_page 404 / 404.html # redirect server error pages to the static page / 50x.html # the first step in error 50 is to visit 50x.html error_page 500502503504 / 50x.hml; # the following is another mapping of 50x.html to access 50x.html location = / 50x.html {root html in the html in the root directory } # proxy the PHP scripts to Apache listening on 127.0.0.1 location 80 # # location ~\ .php$ {# proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1 fastcgi_pass 9000 # # location ~\. Php$ {# root html; # fastcgi_pass 127.0.0.1 root html; 9000; # fastcgi_index index.php; # fastcgi_pass / 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 # ssl_prefer_server_ciphers on; # location / {# root html; # index index.html index.htm; #} #}}

4. Realize the reverse proxy under Nginx.

"do a reverse proxy:"

It means that if the suffix you visit ends with .jpg, then visit another server below

Location ~\ .jpg$ {proxy_pass http://10.7.182.110:8080;}

5. Realize the load balancing under Nginx

"the first strategy: the default is a round-robin policy:"

Configure upstream upstream qianyu {server 10.7.182.110 server 8080; server 10.7.182.87 server 8080;} configure load balancer location ~\ .jpg$ {proxy_pass http://qianyu;}

"the second strategy: weight:"

# configuration of load balancer upstream qianyu {server 10.7.182.110 server 8080 weight=2; server 10.7.182.87 server 8080 weight=1;} configure load balancer location ~. Jpg$ {proxy_pass http://qianyu;}

"third strategy: the use of IPHash:"

# configuration of load balancer upstream qianyu {ip_hash; server 10.7.182.110 http://qianyu; 8080; server 10.7.182.87 ip_hash; server 8080;} configure load balancer location ~. Load balancer {proxy_pass http://qianyu;}

6. Realize the separation of movement and movement under Nginx.

"Separation of movement and movement:"

To put it simply, it separates dynamic resources from static resources.

Put static resources on the Nginx server

Put dynamic resources on the Tomcat server

Jpg html css png gif.... Static resources-> on the Nginx server. Action ends with dynamic resources-- > on the Tomcat server.

"the realization of the separation of movement and movement:"

Create a webapp folder under the / usr/local directory

Create css, html, js, img folders under the webapp directory

Write the contents of HTML and put the html file in the webapp directory

Click me to access dynamic resources

Put the picture in the img directory, put the css in the css directory, and put the js file in the js directory

Write this project for dynamic resources

Package com.qy.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class qianyuServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost (request, response);} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType ("text/html Charset=utf-8 "); PrintWriter writer=response.getWriter (); writer.write (" this is dynamic resource test "); writer.flush (); writer.close ();}}

Write configuration file / conf/nginx.xml file

# Virtual machine server {listen 9999; server_name localhost; # represents the machine location / {proxy_pass http://10.7.182.54:8080; for dynamic resource access } # indicates the address location ~. *\. (htm | html | gif | jpg | jpeg | png | bmp | swf | ioc | rar | zip | txt | mid | doc | ppt | pdf | xls | mp3 | wma) ${root / usr/local/webapp; expires 30d;} # represents the access address location ~. *\ of css and js (js | css)? ${root / usr/local/webapp; expires 1h

test

7. Virtual host

"Virtual host configuration:"

Server {listen 90; server_name localhost; location / {root qianyu; index qianyu.html } # do a reverse proxy # means that if the suffix you visit ends with .jpg, then visit another server below location ~\ .jpg$ {proxy_pass http://10.7.182.110:8080; }} at this point, I believe you have a deeper understanding of "what is the concept of Nginx". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report