当前位置 : 主页 > 编程语言 > java >

Nginx基础入门

来源:互联网 收集:自由互联 发布时间:2022-07-19
nginx介绍 Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的

nginx介绍

  • Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,公开版本1.19.6发布于2020年12月15日。 [12]
  • 其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、简单的配置文件和低系统资源的消耗而闻名。2022年01月25日,nginx 1.21.6发布。 [13]
  • Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好。

nginx安装

  • 安装编译工具及库文件
[root@xy-centos ~]# yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

image.png

  • 下载并安装 PCRE

    [root@xy-centos ~]# cd /opt/ [root@xy-centos opt]# mkdir nginx [root@xy-centos opt]# cd nginx/ [root@xy-centos nginx]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

image.png

[root@bogon src]# tar zxvf pcre-8.35.tar.gz

image.png

  • 进入安装包目录,编译并安装
[root@xy-centos nginx]# cd pcre-8.35 [root@xy-centos pcre-8.35]# ./configure

image.png

[root@xy-centos pcre-8.35]# make && make install

image.png

  • 查看pcre版本,检查是否安装成功
[root@xy-centos pcre-8.35]# pcre-config --version

image.png

  • 下载解压并安装nginx
[root@xy-centos pcre-8.35]# cd .. [root@xy-centos nginx]# wget https://nginx.org/download/nginx-1.22.0.tar.gz

image.png

[root@xy-centos nginx]# cd nginx-1.22.0/ [root@xy-centos nginx-1.22.0]# ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/opt/nginx/pcre-8.35

image.png

[root@xy-centos nginx-1.22.0]# make && make install

image.png

  • 查看nginx版本
[root@xy-centos nginx-1.22.0]# /usr/local/webserver/nginx/sbin/nginx -V

image.png

nignx 常用命令

  • 查看nginx版本
[root@xy-centos sbin]# ./nginx -v

image.png

  • 启动nginx(开启端口号),网页查看nginx是否启动成功
[root@xy-centos sbin]# ./nginx [root@xy-centos sbin]# firewall-cmd --zone=public --list-ports 15672/tcp 8080/tcp 6677/tcp 3306/tcp 801-802/tcp 8001-8002/tcp [root@xy-centos sbin]# firewall-cmd --permanent --add-port=80/tcp success [root@xy-centos sbin]# firewall-cmd --reload success

image.png

  • 重启nginx
[root@xy-centos sbin]# ./nginx -s reload
  • 关闭nginx
[root@xy-centos sbin]# ./nginx -s stop

核心配置文件

[root@xy-centos ~]# vim /usr/local/webserver/nginx/conf/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 1024; } 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; # 负载均衡 upstream myserver{ # 轮询 #server 162.14.108.22:8080; #server 182.61.146.110:8080; # weight #server 162.14.108.22:8080 weight=5; #server 182.61.146.110:8080 weight=10; # ip_hash #ip_hash; #server 162.14.108.22:8080; #server 182.61.146.110:8080; #fair; server 162.14.108.22:8080; server 182.61.146.110:8080; } server { listen 80; server_name localhost; #server_name 162.14.108.22; #charset koi8-r; #access_log logs/host.access.log main; #静态代理 location /www/ { root /data/; index index.html index.thm; } #静态代理 location /image/ { root /data/; autoindex on; index index.html index.thm; } location /{ root html; # proxy_pass http://162.14.108.22:8080; # 反向代理 proxy_pass http://myserver; # 开启负载均衡 index 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 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; # 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 #反向代理2 server { listen 9001; # listen somename:8080; # server_name somename alias another.alias; server_name 162.14.108.22; location ~ /edu/ { proxy_pass http://182.61.146.110:8080; } location ~ /vod/ { proxy_pass http://162.14.108.22:8080; } location ~ /ssm-crud-1.0 { proxy_pass http://162.14.108.22:8080; } } # 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; # } #} }
网友评论