1. Download and install
1. Download from the official website
Liverpoolfc.tv: nginx.org/en/download…
2. Upload the downloaded nginx-1.16.1.tar.gz file to a specified file in Linux or Ubuntu
3. The nginx installation
tar -zxvf nginx-1.16.1.tar.gz / /
cd nginx-1.161.// Switch the directory
./configure --prefix=/usr/local/nginx // configures nginx to the /usr/local/ngnix directory
make / / compile
make install / / installation
Copy the code
4. Common nginx commands
View the nginx installation location whereis nginx vim configuration file/usr/local/nginx/conf/nginx. Conf
See if any nginx process started ps – ef | grep nginx
Start the service/usr/local/nginx/sbin/nginx stop/restart/usr/local/nginx/sbin/nginx -s stop (quit, reload) to reload the configuration /usr/local/nginx/sbin/nginx -s reload
Start, stop, reload, restart, view the status service
$ sudo systemctl status nginx
$ sudo systemctl start nginx
$ sudo systemctl stop nginx
$ sudo systemctl restart nginx
$ sudo systemctl reload nginx
$ sudo systemctl enable nginx
$ sudo systemctl disable nginx
Copy the code
Test whether or not the configuration file/usr/local/right nginx/sbin/nginx – t
5. Visit nginx
After nginx is enabled, access the server IP address locally, which is the server’s localhost. If you cannot access nginx, the firewall is not disabled. The Internet cannot be accessed after nginx is successfully configured
Setting up a firewall:
Ubuntu: $sudo ufw allow'Nginx Full'
$ sudo ufw status
Copy the code
Disable the firewall. [root@localhost ~]# service iptables stopDisable firewall startup after startup [root@localhost ~]# chkconfig iptables off
[root@localhost ~]# chkconfig --list|grep iptMethod 2 Add enabled ports to the firewall whitelist. Edit the firewall whitelist. [root@localhost ~]# vim /etc/sysconfig/iptables-a INPUT -p TCP -m state -- state NEW -m TCP --dport 80 -j ACCEPT save and exit, restart firewall [root@localhost ~]# service iptables restart
Copy the code
Nginx load balancing configuration
Edit the configuration file: vim conf/nginx.conf
Each server is a virtual host that we can use as a Web server
server { listen 8001; Server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; // static file js, CSS, image, location / {root HTML; // Represents the root directory of the site index index.html index.htm; // represents default home page}}Copy the code
Upstream {} block. Nginx uses polling, IP hash, and weights as load balancing policies
upstream xxx{}; Upstream module is used to name a back-end server group. The group name must be the domain name of the back-end server site. IP addresses and ports of multiple servers can be written internally, and ip_hash can be set for jump rules and weights. Indicates that IP address assignment is used to redirect to the back-end server. The same IP address request accesses the same back-end server each time. Represents the address of the back-end server
server{}; The server module is still the part that receives external requests. Location / {}; Also represents filters, used to specify different operations for different requests proxy_pass; Indicates the name of the back-end server group. The group name must be the domain name of the back-end server site
The group names of server_name and upstream{} can be inconsistent. Server_name is the domain name used to receive requests from the Internet, while upstream{} is the domain name used to access the back-end server
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream web{ #ip_hash; Server 192.168.43.31:8080; } server { listen 80; server_name wzj.test.com; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://web; }}}Copy the code
3. Some problems with nginx installation
Make [1]: Leaving directory ‘/usr/local/nginx-1.12.1; After the installation, leave the file;
Make [1]: Leaving directory ‘/usr/local/nginx-1.12.1; Leave it alone and continue to make install; /usr/local/nginx = /usr/local/nginx = /usr/local/nginx 2, If you already have nginx folder? Nginx can start normally. 4, If nginx starts successfully, let’s see if the website is accessible and if “Welcome to nginx!” ? 5, If the above all successful, your nginx has been ok!
Cc1: All warnings being treated as errors objs/Makefile:460: recipe for target ‘objs/src/core/ngx_murmurhash.o’ failed make[1]: [objs/src/core/ngx_murmurhash.o] Error 1 make[1]: Brigade directory ‘/ home/WZJ/tools/nginx/nginx – 1.11.3’ Makefile: 8: the recipe for target ‘build’ failed to make: [build] Error 2
Solution: Find the Maakefile file and remove -werror from GCC.
[root@localhost nginx]# systemctl status nginx.service ● nginx.service – LSB: starts the nginx Web server Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled) Active: failed (Result: exit-code) since Mon 2019-07-22 16:41:54 CST; 43s ago Docs: man:systemd-sysv-generator(8) Process: 28076 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=1/FAILURE)
Jul 22 16:41:54 localhost.localdomain systemd[1]: Starting LSB: starts the nginx web server…
Jul 22 16:41:54 localhost.localdomain nginx[28076]: Starting nginx… nginx (pid 26717 26716
26715 26042) already running.
Jul 22 16:41:54 localhost.localdomain systemd[1]: nginx.service: control process exited,
code=exited status=1
Jul 22 16:41:54 localhost.localdomain systemd[1]: Failed to start LSB: starts the nginx web
server.
Jul 22 16:41:54 localhost.localdomain systemd[1]: Unit nginx.service entered failed state.
Jul 22 16:41:54 localhost.localdomain systemd[1]: nginx.service failed.
Solution: [root @ localhost nginx] # ps -e | grep nginx 26042? 00:00:00 nginx 26715 ? 00:00:00 nginx 26716 ? 00:00:00 nginx 26717 ? 00:00:00 nginx [root@localhost nginx]# kill -9 26042 [root@localhost nginx]# kill -9 26715 [root@localhost nginx]# kill -9 26716 [root@localhost nginx]# kill -9 26717 [root@localhost nginx]# ps -e | grep nginx
Nginx location matching: Nginx location matching: Nginx location matching