Using nginx as front-end server
introduce
Nginx is a lightweight and high-performance Web server / reverse proxy server and email (IMAP/POP3) proxy server, characterized by less memory and strong concurrency
install
Because the dependency libraries of g + +, gcc, OpenSSL devel, PCRE devel and zlib devel are needed during the installation of nginx
So install the required dependency library first
1) Installation dependency
yum install gcc-c++ yum install pcre pcre-devel yum install zlib zlib-devel yum install openssl openssl--devel
2) Download nginx
wget http://nginx.org/download/nginx-1.15.5.tar.gz
3) Unzip
Decompress: tar - zxf nginx-1.15.5 tar.gz Create a nginx-1.15.5 directory and enter the nginx-1.15.5 directory
4) Compile and install
compile: . / configure ා the default installation directory is / usr/local If you want to specify the installation directory, add -- prefix = specify directory install make make install
5) Start
Find our installation directory. If it is the default directory, it is / usr/local/nginx. If there is sbin/nginx in the nginx directory, it is the startup file of nginx. Execute under nginx
Start: ./sbin/nginx stop it: ./sbin/nginx -s stop Restart: ./sbin/nginx -s reload
6) Some compiling options are given, which can be selected but not used in general
--prefix=path defines a directory to store the files on the server, that is, the installation directory of nginx. Use / usr/local / by default; --with-pcre=/pcre-8.37 set the source path of PCRE library. The source code of PCRE library needs to be downloaded from PCRE website and decompressed. --With zlib = / zlib-1.2.8 set the source path of zlib library. To download from zlib and unzip. --with-openssl=/openssl-1.0.1t --without-http_gzip_module does not compile the response module of the compressed HTTP server. zlib is required to compile and run this module; --without-http_rewrite_module does not compile rewrite modules. PCRE library is needed to compile and run this module; --without-http_proxy_module does not compile http_proxy module; --with-http_ssl_module uses https protocol module. By default, the module is not built. It is necessary to build and run the OpenSSL Library of this module;
7) Post three links to avoid looking for them later
pcre: http://ftp.pcre.org/pub/pcre http://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz zlib: http://www.zlib.net/fossils/ http://www.zlib.net/fossils/zlib-1.2.8.tar.gz openssl: https://ftp.openssl.org/source http://distfiles.macports.org/openssl/openssl-1.0.1k.tar.gz
8) Special circumstances
Our first step is to provide a compilation environment for the installation, and the operation of nginx does not depend on the library of step 1. If one day you have a server that cannot connect to the Internet and you need to install nginx, and there is no compilation environment required by nginx on it, our first idea at this time is to compile according to the above environment, but because of which As like as two peas, the nginx is a good example of nginx. In some cases, we can choose to build a library that is not easy to fix in offline mode. (except for one integrated package), in this case, we can choose to skip the compilation process and package the compiled nginx packages onto the non extranet server on another Internet device.
configuration file
The configuration file of nginx is in conf under the nginx directory/ nginx.conf
Default profile
#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 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; } # 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; # } #} }
server
One server listens to one port, and one nginx can open multiple ports
server { listen 80; #Listening port server_name localhost; location / { #route root html; #root directory index index.html index.htm; #Follow the document } location /api { rewrite ^/api/(.*)$ /$1 break; #redirect proxy_pass http://127.0.0.1:8080; agent } }
Project deployment
The html directory in the nginx directory is where we put static files, for example, we put official_ Put website under html
We just need to modify it nginx.conf The location of
location / { root html/official_website; index index.html index.htm; }
Common Linux commands
ls traverses files in the current directory -l traverse the files and directories in the current directory, and display the file details (excluding hidden files) -a traverse all current files and directories (including hidden files) cd into a directory mkdir create a directory touch creates a file cat view the contents of a file rm delete -r directory can be deleted -f skip confirmation pwd get the path of the current directory scp file transfer -r upload multiple files or directories For example: File upload: scp file user@IP:path File download: scp user@IP:filePath path tar compression and decompression Unzip: tar -xf filePath.tar Compression: tar - CF filePath.tar filePath Common text editor of vi/vim There are two common modes: edit mode and command line mode. After entering, the default mode is command line mode i: Enter edit mode esc: enter command line mode In command line mode: q: Exit w: Save !: forced