欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Nginx学习笔记

时间:2023-06-28
适用场景 http 的反向代理服务器

正向代理(给客户端用的)


反向代理(给服务端用的)

安全(和正向功能类似)、后端多个服务器做负载均衡

正向+反向代理

动态静态资源分离

静态资源无需经过 tomcat,tomcat 只负责处理动态请求

优点

1、高并发,高性能
2、可扩展性好,模块化
3、高可靠性
4、热部署,在不重启的情况下更新
5、开源,可商用

安装

yum install yum-utils

vi /etc/yum.repos.d/nginx.repo[nginx-stable]name=nginx stable repobaseurl=http://nginx.org/packages/centos/7/$basearch/gpgcheck=1enabled=1gpgkey=https://nginx.org/keys/nginx_signing.keymoudle_hosfixes=true[nginx-mainline]name=nginx mainline repobaseurl=http://nginx.org/packages/mainline/centos/7/$basearch/gpgcheck=1enabled=0gpgkey=https://nginx.org/keys/nginx_signing.keymoudle_hosfixes=true

yum list | grep nginxnginx.x86_64 1:1.20.2-1.el7.ngx nginx-stablenginx-debug.x86_64 1:1.8.0-1.el7.ngx nginx-stablenginx-debuginfo.x86_64 1:1.20.2-1.el7.ngx nginx-stablenginx-module-geoip.x86_64 1:1.20.2-1.el7.ngx nginx-stablenginx-module-geoip-debuginfo.x86_64 1:1.20.2-1.el7.ngx nginx-stablenginx-module-image-filter.x86_64 1:1.20.2-1.el7.ngx nginx-stablenginx-module-image-filter-debuginfo.x86_64 1:1.20.2-1.el7.ngx nginx-stablenginx-module-njs.x86_64 1:1.20.2+0.7.2-1.el7.ngx nginx-stablenginx-module-njs-debuginfo.x86_64 1:1.20.2+0.7.2-1.el7.ngx nginx-stablenginx-module-perl.x86_64 1:1.20.2-1.el7.ngx nginx-stablenginx-module-perl-debuginfo.x86_64 1:1.20.2-1.el7.ngx nginx-stablenginx-module-xslt.x86_64 1:1.20.2-1.el7.ngx nginx-stablenginx-module-xslt-debuginfo.x86_64 1:1.20.2-1.el7.ngx nginx-stablenginx-nr-agent.noarch 2.0.0-12.el7.ngx nginx-stablepcp-pmda-nginx.x86_64 4.3.2-13.el7_9 updates

yum install nginx 1:1.20.2-1.el7.ngx

nginx -vnginx version: nginx/1.20.2

常用命令 启动

/usr/sbin/nginx

停止

立即停止

nginx -s stop

优雅停止(线上)

nginx -s quit

重启

使用的优雅重启

nginx -s reload

指定配置文件

nginx -c /etc/nginx/nginx.conf

-c 读取指定配置文件

测试

检查当前 nginx 使用的是哪个配置文件;检查配置文件的书写格式是否符合规范

nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful

配置文件

以分号结尾

以{}组织多条指令(参考 http)

include 引入

井号注释

$ 变量

/etc/nginx/nginx.conf

user nginx;worker_processes auto;error_log /var/log/nginx/error.log notice;pid /var/run/nginx.pid;events { # 最大连接数 worker_connections 1024;}http { include /etc/nginx/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 /var/log/nginx/access.log main;# 高效传输 sendfile on; #tcp_nopush on;# 长连接超时时间 keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf;}

/etc/nginx/conf.d/default.conf

server { # 指定 80 端口 listen 80; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; 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 /usr/share/nginx/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; #}}

/usr/share/nginx/html/index.html

访问 80 端口默认展示的页面

新建一个 test.html

在 /usr/share/nginx/html/ 目录下新建一个 test.html,然后通过 ip:80/test.html 即可直接访问(或者 ip/test.html)

搭建一个静态文件的 nginx 服务

在 /usr/share/nginx/ 目录下新建 test 文件夹

vi /etc/nginx/conf.d/default.conf

server { # 指定 80 端口 listen 80; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { # *****************修改项***************** # 将 html 修改为 test root /usr/share/nginx/test; 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 { # *****************修改项***************** # 将 html 修改为 test root /usr/share/nginx/test; } # 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; #}}

在 /usr/share/nginx/test 目录下放一个 test.html,重启 nginx,之后即可通过 ip/test.html 访问

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。