什么是Nginx

  • Nginx (engine x) 是一个高性能的HTTP反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。

  • 其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好!

  • Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,公开版本1.19.6发布于2020年12月15日


Nginx的作用

  • Http代理,反向代理:作为web服务器最常用的功能之一,尤其是反向代理

    正向代理

 

 

 

反向代理

 

 

  • 负载均衡

    nginx提供两种的两种的的负载均衡策略。内置策略为轮询,加权轮询,Ip hash。扩展策略,就天马行空,只有你想不到,没有他做不到的!!!

    轮询image-20220416174258739

    加权轮询image-20220416174332512

    Iphash

    iphash对客户端请求的ip进行hash操作,然后根据hash的结果将同一个客户Ip请求分发同一台服务器进行处理,已解决session不贡献的问题!

    image-20220416174720207

    动静分离

    在我们软件开发过程中,有些请求需要后台去处理的,有些是不需要后台进行处理的(如:ccs、html、jpg、js等等文件),这些不需要后处理的就叫做静态文件让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源分开来,动静资源放开了以后,我们就可以根据静态的特点将其做缓存的操作,提高资源的响应速度!

    image-20220416175337720


Nginx安装

nginx 1.18.0稳定版 官网下载地址:http://nginx.org/en/download.html

wget http://nginx.org/download/nginx-1.18.0.tar.gz

tar -zxf nginx-1.18.0.tar.gz

cd nginx-1.18.0

./configure

make

make install

/usr/local/nginx/sbin/nginx

[root@localhost ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
--2022-04-16 06:22:47-- http://nginx.org/download/nginx-1.18.0.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1039530 (1015K) [application/octet-stream]
Saving to: ‘nginx-1.18.0.tar.gz’

100%[==============================================================================>] 1,039,530   672KB/s   in 1.5s

2022-04-16 06:22:49 (672 KB/s) - ‘nginx-1.18.0.tar.gz’ saved [1039530/1039530]

[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.18.0.tar.gz
[root@localhost ~]# tar -zxf nginx-1.18.0.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.18.0 nginx-1.18.0.tar.gz
[root@localhost ~]# cd nginx-1.18.0
[root@localhost nginx-1.18.0]# ll
total 768
drwxr-xr-x. 6 1001 1001   4096 Apr 16 06:22 auto
-rw-r--r--. 1 1001 1001 302863 Apr 21  2020 CHANGES
-rw-r--r--. 1 1001 1001 462213 Apr 21  2020 CHANGES.ru
drwxr-xr-x. 2 1001 1001   4096 Apr 16 06:22 conf
-rwxr-xr-x. 1 1001 1001   2502 Apr 21  2020 configure
drwxr-xr-x. 4 1001 1001     68 Apr 16 06:22 contrib
drwxr-xr-x. 2 1001 1001     38 Apr 16 06:22 html
-rw-r--r--. 1 1001 1001   1397 Apr 21  2020 LICENSE
drwxr-xr-x. 2 1001 1001     20 Apr 16 06:22 man
-rw-r--r--. 1 1001 1001     49 Apr 21  2020 README
drwxr-xr-x. 9 1001 1001     84 Apr 16 06:22 src
[root@localhost nginx-1.18.0]# ./configure
checking for OS
+ Linux 3.10.0-327.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
Configuration summary
 + using system PCRE library
 + OpenSSL library is not used
 + using system zlib library

nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
[root@localhost nginx-1.18.0]# make
make -f objs/Makefile
make[1]: Entering directory `/root/nginx-1.18.0'
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
       -o objs/src/core/nginx.o \
      src/core/nginx.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
       -o objs/src/core/ngx_log.o \
      src/core/ngx_log.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
       -o objs/src/core/ngx_palloc.o \
      src/core/ngx_palloc.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
       -o objs/src/core/ngx_array.o \
      src/core/ngx_array.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
       -o objs/src/core/ngx_list.o \
      src/core/ngx_list.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
       -o objs/src/core/ngx_hash.o \
      src/core/ngx_hash.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
       -o objs/src/core/ngx_buf.o \
      src/core/ngx_buf.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
       -o objs/src/core/ngx_queue.o \
      src/core/ngx_queue.c
       -ldl -lpthread -lcrypt -lpcre -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
       -e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
       -e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
       -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
      < man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/root/nginx-1.18.0'
[root@localhost nginx-1.18.0]# mdke install
-bash: mdke: command not found
[root@localhost nginx-1.18.0]# make install
make -f objs/Makefile install
make[1]: Entering directory `/root/nginx-1.18.0'
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' \
      || mkdir -p '/usr/local/nginx/sbin'
test ! -f '/usr/local/nginx/sbin/nginx' \
      || mv '/usr/local/nginx/sbin/nginx' \
               '/usr/local/nginx/sbin/nginx.old'
cp objs/nginx '/usr/local/nginx/sbin/nginx'
test -d '/usr/local/nginx/conf' \
      || mkdir -p '/usr/local/nginx/conf'
cp conf/koi-win '/usr/local/nginx/conf'
cp conf/koi-utf '/usr/local/nginx/conf'
cp conf/win-utf '/usr/local/nginx/conf'
test -f '/usr/local/nginx/conf/mime.types' \
      || cp conf/mime.types '/usr/local/nginx/conf'
cp conf/mime.types '/usr/local/nginx/conf/mime.types.default'
test -f '/usr/local/nginx/conf/fastcgi_params' \
      || cp conf/fastcgi_params '/usr/local/nginx/conf'
cp conf/fastcgi_params \
       '/usr/local/nginx/conf/fastcgi_params.default'
test -f '/usr/local/nginx/conf/fastcgi.conf' \
      || cp conf/fastcgi.conf '/usr/local/nginx/conf'
cp conf/fastcgi.conf '/usr/local/nginx/conf/fastcgi.conf.default'
test -f '/usr/local/nginx/conf/uwsgi_params' \
      || cp conf/uwsgi_params '/usr/local/nginx/conf'
cp conf/uwsgi_params \
       '/usr/local/nginx/conf/uwsgi_params.default'
test -f '/usr/local/nginx/conf/scgi_params' \
      || cp conf/scgi_params '/usr/local/nginx/conf'
cp conf/scgi_params \
       '/usr/local/nginx/conf/scgi_params.default'
test -f '/usr/local/nginx/conf/nginx.conf' \
      || cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf'
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' \
      || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' \
      || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
      || cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
      || mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory `/root/nginx-1.18.0'
[root@localhost nginx-1.18.0]# /usr/local/nginx/sbin/nginx

安装时 ./configure报错

[root@localhost nginx-1.18.0]# ./configure
checking for OS
+ Linux 3.10.0-327.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found

是因为缺少环境变量

解决办法

yum -y install gcc;yum -y install gcc-c++

[root@localhost nginx-1.18.0]# yum -y install gcc;yum -y install gcc-c++
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package gcc.x86_64 0:4.8.5-44.el7 will be installed
--> Processing Dependency: libgomp = 4.8.5-44.el7 for package: gcc-4.8.5-44.el7.x86_64
--> Processing Dependency: cpp = 4.8.5-44.el7 for package: gcc-4.8.5-44.el7.x86_64
--> Processing Dependency: libgcc >= 4.8.5-44.el7 for package: gcc-4.8.5-44.el7.x86_64
--> Processing Dependency: glibc-devel >= 2.2.90-12 for package: gcc-4.8.5-44.el7.x86_64
--> Processing Dependency: libmpfr.so.4()(64bit) for package: gcc-4.8.5-44.el7.x86_64
--> Processing Dependency: libmpc.so.3()(64bit) for package: gcc-4.8.5-44.el7.x86_64
--> Running transaction check
---> Package cpp.x86_64 0:4.8.5-44.el7 will be installed
---> Package glibc-devel.x86_64 0:2.17-325.el7_9 will be installed
--> Processing Dependency: glibc-headers = 2.17-325.el7_9 for package: glibc-devel-2.17-325.el7_9.x86_64
--> Processing Dependency: glibc = 2.17-325.el7_9 for package: glibc-devel-2.17-325.el7_9.x86_64
--> Processing Dependency: glibc-headers for package: glibc-devel-2.17-325.el7_9.x86_64
---> Package libgcc.x86_64 0:4.8.5-4.el7 will be updated
---> Package libgcc.x86_64 0:4.8.5-44.el7 will be an update
---> Package libgomp.x86_64 0:4.8.5-4.el7 will be updated
---> Package libgomp.x86_64 0:4.8.5-44.el7 will be an update
Total                                                                                   202 kB/s | 9.0 MB  00:00:45
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Updating   : libstdc++-4.8.5-44.el7.x86_64                                                                        1/4
Installing : libstdc++-devel-4.8.5-44.el7.x86_64                                                                  2/4
Installing : gcc-c++-4.8.5-44.el7.x86_64                                                                          3/4
Cleanup   : libstdc++-4.8.5-4.el7.x86_64                                                                         4/4
Verifying : gcc-c++-4.8.5-44.el7.x86_64                                                                          1/4
Verifying : libstdc++-devel-4.8.5-44.el7.x86_64                                                                  2/4
Verifying : libstdc++-4.8.5-44.el7.x86_64                                                                        3/4
Verifying : libstdc++-4.8.5-4.el7.x86_64                                                                         4/4

Installed:
gcc-c++.x86_64 0:4.8.5-44.el7

Dependency Installed:
libstdc++-devel.x86_64 0:4.8.5-44.el7

Dependency Updated:
libstdc++.x86_64 0:4.8.5-44.el7

Complete!

还是报错

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.

解决

yum -y install openssl openssl-devel

测试:

[root@localhost nginx-1.18.0]# curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
  body {
      width: 35em;
      margin: 0 auto;
      font-family: Tahoma, Verdana, Arial, sans-serif;
  }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

先关闭防火墙

[root@localhost nginx-1.18.0]# systemctl stop firewalld
[root@localhost nginx-1.18.0]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.

不关闭防火墙,也可以放行相对应的端口

# 查询端口是否开放
firewall-cmd --query-port=8080/tcp
# 开放80端口
firewall-cmd --permanent --add-port=80/tcp
# 移除端口
firewall-cmd --permanent --remove-port=8080/tcp
#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload

 

image-20220416184807204

nginx常用命令


cd /usr/local/nginx/sbin
./nginx    #启动nginx服务
./nginx -c 文件名          #制定加载的配置文件,默认时候conf/nginx.conf
./nginx -s stop  #停止nginx服务
./nginx -s quit  #安全退出
./nginx -s reload  #重新加载配置文件
./nginx -g "pid logs/文件名.pid;"  #将nginx的进程号写入到文件名.pid文件中,默认保存到、logs/nginx.pid
ps -aux | grep nginx      #查看nginx的进程
kill -quit 进程号          #停止某进程,比较安全的退出
kill -term 进程号 #强制退出  
kill -9 进程号 #强制退出

修改nginx的监听端口


[root@localhost ~]# cd /usr/local/nginx/
[root@localhost nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@localhost nginx]# vim conf/nginx.conf
##############################################

   #gzip on;

  server {
      listen       80;
改为
   #gzip on;

  server {
      listen       8080;
[root@localhost nginx]# ./sbin/nginx -s reload

测试

[root@localhost nginx]# curl localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
  body {
      width: 35em;
      margin: 0 auto;
      font-family: Tahoma, Verdana, Arial, sans-serif;
  }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

 

posted on   乐笙  阅读(32)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)



点击右上角即可分享
微信分享提示