Install and configure Nginx reverse proxy under Linux

Preparations before installation

Because Nginx depends on the gcc compilation environment, you need to install the compilation environment to enable Nginx to compile

yum install gcc-c++

yum install -y openssl openssl-devel

Nginx's http module needs to use pcre to parse regular expressions , and pcre needs to be installed

yum install -y pcre pcre-devel

Then install the zlib package

yum install -y pcre zlib-devel

Download package + unzip file

wget -c https://nginx.org/download/nginx-1.20.2.tar.gz

tar -zxvf nginx-1.20.2.tar.gz

Enter the file cd nginx-1.20.2

run

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

Install

make & make install

Note that after the installation is successful, the exit directory will be displayed here

Go to the conf directory and configure the nginx.conf file

vim nginx.conf

#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  65535;
}


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;
    #The minimum number of bytes allowed to be compressed
    gzip_min_length 1k;
    #4 units of 16k memory are used as the compressed result stream cache
    gzip_buffers 4 16k;
    #Setting to identify the HTTP protocol version, the default is 1.1
    gzip_http_version 1.1;
    #gzip compression ratio, available Set among 1~9, 1 has the smallest compression ratio and the fastest speed, 9 has the largest compression ratio and the slowest speed, and consumes CPU
    gzip_comp_level 2;
    #Compressed type
    gzip_types text/plain application/x-javascript text/css application/xml;
    #Let the front-end cache server mix the gzip-compressed pages
    gzip_vary on;


    # Configure forwarding to port 8700
    upstream huida{       server 127.0.0.1:8700;     }

    server {         listen 80;         listen 443 ssl; # Configure https, listen to port 433         server_name xxx.xxx; # Note that if you apply for a domain name configuration, you can only access https if you configure a certificate


        error_page 405 =200 $request_uri;
       
        ssl_certificate  cert/7629385.pem;
        ssl_certificate_key cert/7629385.key;

        client_max_body_size 50m;
        underscores_in_headers on;

        proxy_set_header Host      $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        index index.htm index.html index.php;

        proxy_connect_timeout 60; #Establish the connection time of the tcp protocol
        proxy_send_timeout 60; #Send the time of the interface
        proxy_read_timeout 60; #Read time (interface response time)

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

        
        # Configure forwarding
      location /huida/ {

                 add_header 'Access-Control-Allow-Origin' '*';
                 add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                 add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';
                 add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';

              proxy_pass http://huida;
         }


        location / {
            root   /home/html/huida/;
            index  index.html index.htm;
        }


        #Static files are handed over to nginx to process proxy front-end static resource
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls| mp3|wma)$
        {

         root /home/html/huida/;
                expires 12;
        }

        #Static files are handed over to nginx for processing
        location ~ .*\.(js|css)?$
        {           root /home/html/huida/;

            expires 15d;
        }

        #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;
    #    }
    #}
    }

 

 

 

Guess you like

Origin blog.csdn.net/weixin_59539033/article/details/127747149
Recommended