Simple Installation and Construction of nginx Server under CentOS 7

Posted by scrappy1855 on Sat, 27 Jul 2019 13:28:44 +0200

Simple Installation and Construction of nginx Server under CentOS 7

Memory is better than typing on the keyboard. Mengxin started writing his first blog today.
Refer to some other CSDN bosses'articles, record what you have learned, act as an experimental record.

The main purpose of the experiment

  • 1. Install nginx server
  • 2. Configure https protocol for server, ssl certificate and domain name and project
  • 3. Start the nginx server to provide request forwarding function for tomcat server

Finally, it can input the domain name of the website and display the effect of the project directly.

Experimental environment

  • CentOS 7.5
  • There is a tomcat server that has started running the project, port 8080
  • Domain name and ssl certificate have been purchased

The first step is to install nginx

1. Download the nginx server installation package. Take nginx 1.14.2. tar. GZ as an example, place the file in the / usr/local directory.
2. Unzip the installation package into the directory and enter the unzipped folder.

[root@centos local]# tar -xzvf nginx-1.14.2.tar.gz
[root@centos local]# cd nginx-1.14.2

3. Configuration and installation

//This is the most original installation without any functional components.
[root@centos nginx-1.14.2]# ./configure && make && make install

//To support the https protocol, it is recommended to install openssl first with the following installation
[root@centos nginx-1.14.2]# yum -y install openssl openssl-devel
[root@centos nginx-1.14.2]# ./configure --with-http_ssl_module
[root@centos nginx-1.14.2]# make
[root@centos nginx-1.14.2]# make install

At this point, nginx is installed and installed in the / usr/local directory by default.
If you first perform the original installation and later configure ssl, there will be a problem. Just turn off the nginx server and go back to the nginx-1.14.2 folder. Install and overwrite it again in the second way. Pay attention to backing up the configuration file.

Step 2, nginx configuration

After installation, a nginx folder appears under local and clicks in
Re-enter the conf folder inside
The command line is directly cd/usr/local/nginx/conf
There is a nginx.conf file in this directory. This is the configuration file of nginx.
Then with ssl certificates, you can put certificates and keys in a directory with nginx.conf, and all you have left is configuration.

The nginx configuration needs special study to master. Here is the configuration file for me directly.
I deleted some comments and did not test the file. Lines with # are the lines that were commented out. There are two web projects running on my server.

worker_processes  1;

events {
    worker_connections  1024;
}

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

    sendfile        on;
    keepalive_timeout  65;
    
    server {
        listen       80;
        listen 443 ssl;
        server_name www.jakuxa.cn ; 		#The domain name here is changed to its own.
        ssl on;												#on and off control ssl switch
        ssl_certificate 1_www.jakuxa.cn_bundle.crt;       #Here is to quote your two certificate files, note the path (currently in the same directory)
        ssl_certificate_key 2_www.jakuxa.cn.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;

        location /testProject/ {
             proxy_pass http://118.89.22.111:8080/testProject/; Here is your first project address, note that you are listening for the default port of your tomcat server.
             proxy_http_version 1.1;
             proxy_set_header Upgrade $http_upgrade;
             proxy_set_header Connection "upgrade";
        }

        location /anotherProject/ {
           # root   html;
           # index  index.html index.htm;
             proxy_pass http://118.89.22.111:8080/another Project/; Your second project address
             proxy_http_version 1.1;
             proxy_set_header Upgrade $http_upgrade;
             proxy_set_header Connection "upgrade";
        }
		
		#If you only have one project, that's it.
			#location /testProject {         #Finally, don't add "/" to it.
	           # root   html;
	           # index  index.html index.htm;
	           #proxy_pass http://118.89.22.111:8080/anotherProject;					
	           #proxy_http_version 1.1;
	           #proxy_set_header Upgrade $http_upgrade;
	           #proxy_set_header Connection "upgrade";
	       #}

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

}

Step 3 Start, Stop and Restart the nginx Server

//Switch to the nginx folder first
[root@centos local]# cd /usr/local/nginx

//Pre-startup checks are prompted if configuration errors occur
[root@centos nginx]# ./sbin/nginx -t

//Start the server, the other way is more troublesome, I suggest this way to follow.
[root@centos nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

//Restart the server and run the following statement every time you change the configuration
[root@centos nginx]# ./sbin/nginx -s reload

//Close the server
[root@centos nginx]# ./sbin/nginx -s quit

Reference material:
https://blog.csdn.net/cxs123678/article/details/80201412
https://www.cnblogs.com/liluxiang/p/9309460.html
https://blog.csdn.net/SCY_Shadow/article/details/79868550
https://blog.csdn.net/dongdong9223/article/details/52299147

Topics: Nginx CentOS SSL Tomcat