Nginx installation

1. Install some auxiliary tools in Linux

`yum -y install gcc gcc-c++ autoconf pcre-devel make automake wget httpd-tools vim `
Copy the code

2. Run CD ~ to switch to the root directory and create a user directory of your own

`cd ~`

`mkdir klierbyck`

`cd klierbyck`

`mkdir app backup soft logs works`
Copy the code

3. View the version of the nginx source

`yum list | grep nginx`
Copy the code

4. Open the http://nginx.org/en/download.html for the latest Stable version

5. Open http://nginx.org/en/linux_packages.html to find the source for the system

6. Create a configuration file for the nginx source

`vim /etc/yum.repos.d/nginx.repo`
Copy the code

7. Modify the following code and save it in nginx.repo to exit

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
Copy the code

8. Check whether the latest stable version is available

`yum list | grep nginx`
Copy the code

9. Install nginx

`yum install nginx`
Copy the code

10. Check whether nginx is successfully installed

`nginx -v`
Copy the code

2, nginx configuration instructions (you can ignore step 1,2,3,4 and start from step 5)

1. Where is nginx installed

`rpm -ql nginx`
Copy the code

2. Check the nginx. Conf

`cd /etc/nginx`

`vim nginx.conf`
Copy the code
# Configuration description
# run user, default is nginx, can not set
user  nginx;
#Nginx process, generally set to the same number of CPU cores
worker_processes  1;   
# Error log directory
error_log  /var/log/nginx/error.log warn;
# Process PID location
pid        /var/run/nginx.pid;
events {
    worker_connections  1024; # Maximum number of concurrent requests for a single background process
}
http {
    include       /etc/nginx/mime.types;   File extension and type mapping table
    default_type  application/octet-stream;  The default file type
    Set the logging mode
    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  /var/log/nginx/access.log  main;   #nginx access log location
    sendfile        on;   Enable efficient transmission mode
    #tcp_nopush on; # Reduce the number of network packet segments
    keepalive_timeout  65;  # Time to hold a connection, also known as timeout
    #gzip on; Enable gzip compression
    include /etc/nginx/conf.d/*.conf; # Contains the location and file of the child configuration item
}
Copy the code

3. Check the default. Conf

`cd /etc/nginx/conf.d`

`vim default.conf`
Copy the code
Server {listen 80; Server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; 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 /usr/share/nginx/ 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; #}}Copy the code

4. Access the program directory on the website

`cd /usr/share/nginx/html`
Copy the code

5. Configure security groups for Aliyun

Enter ali Cloud console and find ECS instance. Click "More" next to the instance, click "Network and Security Groups", and then click "Security Group Configuration" at the upper right corner to add "Security Group Configuration" to set port 80. The details are as shown in the figure.Copy the code

6. Start the nginx

`nginx`
Copy the code

7. You can access the website using the public IP address of the server

Nginx common commands

1. Query the running status of the Nginx service

`ps aux | grep nginx`
Copy the code

2. Start

`nginx`
`systemctl start nginx.service`
Copy the code

3. Stop

`nginx -sStop '(stop service immediately)' nginx-sQuit 'killall nginx' (kill the process) 'systemctl stop nginx.service' (systemctl stop)Copy the code

4. Restart

`systemctl restart nginx.service`
Copy the code

5. Load the file again

`nginx -s reload`
Copy the code

6. Check the enabled port number

`netstat -tln`
Copy the code

Error page and access Settings

vim /etc/nginx/conf.d/default.conf

Error_page 404/404.html; error_page 404 http://www.klierbyck.com; error_page 500 502 503 504 /50x.html; Location / {deny 123.9.51.42; // Allow 45.76.202.231; } location / {allow 45.76.202.231; } # for the img directory on the site, all user access is enabled, but for the admin directory on the site, only internal fixed IP access is allowed. location =/img{ allow all; } location =/admin{allow 192.1.0.12; } # the = sign represents an exact match. Location ~/.php${deny all; } # so we can't access the files at the end of PHPCopy the code

5, Nginx set virtual host

1. Set the virtual host based on the port number

Create an 8001.conf file in /etc/nginx/conf.d/, copy the following code into it and save it

server{ listen 8001; server_name localhost; location / { root /usr/share/nginx/html/html8001; index index.html index.htm; }}Copy the code

Create an html8001 folder under /usr/share/nginx/html/ and place an index.html

Reload nginx to access the site via IP:8001

2. Virtual host based on IP address

Create an ip1.conf file in /etc/nginx/conf.d /, copy the following code into it and save it

server{ listen 80; Server_name IP address; location / { root /usr/share/nginx/html/ip1; index index.html index.htm; }}Copy the code

Create an ip1 folder under /usr/share/nginx/html/ and place an index.html

Reloading nginx allows you to access the site using an IP address

3. Virtual hosts based on domain names

Conf file in /etc/nginx /conf.d/. Copy the following code into the file and save it

server{
	listen 80;
	server_name test.klierbyck.com;
         location / {
            root /usr/share/nginx/html/test; index index.html index.htm; }}Copy the code

Create a test folder under /usr/share/nginx/html/ and place an index.html

Reload nginx to access the site via test.klierbyck.com

Nginx reverse proxy Settings

Create a proxy.conf file in /etc/nginx /conf.d/. Copy the following code into the file and save it

server{ listen 80; server_name proxy.klierbyck.com; location / { proxy_pass http://www.klierbyck.com; }}Copy the code

Reloading nginx allows you to access the site through proxy.klierbyck.com

Some common reverse proxy directives: proxy_set_header: Changes the request header information from the client before sending the request to the back-end server. Proxy_connect_timeout: Specifies the timeout period for Nginx to try to establish a connection with the backend proxy server. Proxy_read_timeout: configures the Nginx backend server group to emitreadAfter the request, wait for the corresponding timeout. Proxy_send_timeout: Specifies the timeout period for Nginx to wait for a write request from a back-end server group. Proxy_redirect: Used to modify Location and Refresh in the response header returned by the back-end server.Copy the code

Nginx for PC or mobile devices

Set up directories for PC and mobile programs

cd /usr/share/nginx
mkdir pc
mkdir mobile
Copy the code

Match the following content into the/etc/nginx/conf. D/default. Conf

server{
	listen 80;
	server_name www.klierbyck.com;
	location / {
		root /usr/share/nginx/pc;
		if ($http_user_agent ~ "(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Brow ser)|(UCWEB)|(SEMC\-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT\-)|(SonyEricsson)|(NEC\-)|(Alcatel)|(Erics son)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi\-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG\- ) | | (LG) to (EG900) | | (CECT) (Compal) | | (kejian) (Bird) | | (Bird) (G900 / V1.0) | | (Arima) (CTL) | | (TDG) (Daxian) | (Daxian) | (DBTEL) | (Eastco m)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC\-)|(SED\-)|(EMOL \-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera)") { root /usr/share/nginx/mobile; } index index.html; }}Copy the code

8. Enable Gzip compression

Run the command to edit nginx.conf

vim etc/nginx/nginx.conf

Add the following configuration items to the HTTP object

http {
	gzip on;
	gzip_types text/plain application/javascript text/css;
}
Copy the code

Gzip more configuration

Gzip: This directive is used to enable or disable the Gzip module. Gzip_buffers: Sets the system to obtain several units of cache for storing gzip's compressed result data stream. Gzip_comp_level: Gzip compression ratio. The compression level ranges from 1 to 9. 1 indicates the lowest compression level, and 9 indicates the highest compression level. The higher the compression level, the higher the compression rate and the longer the compression time. Gzip_disable: This command disables compression for certain User-agents. Gzip_min_length: Sets the minimum number of bytes of a page that can be compressed. The number of bytes of a page is obtained from the content-length of the corresponding header. Gzip_http_version: identifies the HTTP version. The value can be 1.1. Or 1.0.gzip_proxied: Used to enable or disable gzip compression for receiving content from the proxy server. Gzip_vary: Used to add Vary: accept-encoding to the response header so that the proxy server identifies whether gzip compression is enabled based on the accept-encoding in the request header.Copy the code

Nine. Final two configuration files

1./etc/nginx/nginx.conf

# run user, default is nginx, can not set
user nginx;

#Nginx process, generally set to the same number of CPU cores
worker_processes 1;

# Error log directory
error_log /var/log/nginx/error.log warn;

# Process PID location
pid /var/run/nginx.pid;

events {
    # Maximum number of concurrent requests for a single background process
    worker_connections 1024;
}

http {
    File extension and type mapping table
    include /etc/nginx/mime.types;

    The default file type
    default_type application/octet-stream;

    Set the logging mode
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    #nginx access log location
    access_log /var/log/nginx/access.log main;

    Enable efficient transmission mode
    sendfile        on;

    # Reduce the number of network packet segments
    #tcp_nopush on;

    # Time to hold a connection, also known as timeout
    keepalive_timeout  65;

    Enable gzip compression
    gzip on;
    gzip_types text/plain application/javascript text/css;

    # Contains the location and file of the child configuration item
    include /etc/nginx/conf.d/*.conf;
}
Copy the code

2./etc/nginx/conf.d/default.conf

server {
    Configure the listening port
    listen 80;

    # configure domain name
    server_name www.klierbyck.com;

    #charset koi8-r;
    #access_log /var/log/nginx/host.access.log main;

    Configure the program storage location and PC, mobile terminal
    location / {
        root /usr/share/nginx/pc;
        if ($http_user_agent ~ "(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Brow ser)|(UCWEB)|(SEMC\-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT\-)|(SonyEricsson)|(NEC\-)|(Alcatel)|(Erics son)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi\-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG\- ) | | (LG) to (EG900) | | (CECT) (Compal) | | (kejian) (Bird) | | (Bird) (G900 / V1.0) | | (Arima) (CTL) | | (TDG) (Daxian) | (Daxian) | (DBTEL) | (Eastco m)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC\-)|(SED\-)|(EMOL \-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera)" ) {
            root /usr/share/nginx/mobile;
        }
        index  index.html index.htm;
    }

    # error page
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    Vue history mode configuration
    #location / {
    # try_files $uri $uri/ /index.html;
    #}

    # redirect server error pages to the static page /50x.html
    #
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    # Reverse proxy configuration
    # 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;
    #}
}
Copy the code