Nginx 服务器端配置

2022-04-01 15:57:16 浏览数 (1)

这是一篇关于 Nginx 服务器端配置的记录

#nginx 安装(centOS 7)并配置服务

#添加 yum 源

代码语言:javascript复制
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

#安装

代码语言:javascript复制
yum install nginx

#Mac 安装 Nginx

首先在命令行输入 nginx 如果没有任何输出的话,代表你已经安装了 nginx 了

否则进行安装,直接使用 brew 进行安装

代码语言:javascript复制
brew install nginx

跑完之后可以在试一下是否安装成功

TIP

通过 brew 安装的 nginx 可以通过 brew info nginx 查看相关信息

或者通过 nginx -V 来查看相关信息

代码语言:javascript复制
$ brew info nginx

nginx: stable 1.17.10 (bottled), HEAD
HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
https://nginx.org/
/usr/local/Cellar/nginx/1.17.10 (25 files, 2.1MB) *
  Poured from bottle on 2020-10-10 at 15:43:28
From: https://mirrors.ustc.edu.cn/homebrew-core.git/Formula/nginx.rb
==> Dependencies
Required: openssl@1.1 ✔, pcre ✔
==> Options
--HEAD
	Install HEAD version
==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  nginx
==> Analytics
install: 32,211 (30 days), 102,881 (90 days), 409,451 (365 days)
install-on-request: 31,621 (30 days), 100,926 (90 days), 397,605 (365 days)
build-error: 0 (30 days)
代码语言:javascript复制
$ nginx -V

nginx version: nginx/1.17.10
built by clang 11.0.3 (clang-1103.0.32.29)
built with OpenSSL 1.1.1f  31 Mar 2020 (running with OpenSSL 1.1.1g  21 Apr 2020)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/Cellar/nginx/1.17.10 --sbin-path=/usr/local/Cellar/nginx/1.17.10/bin/nginx --with-cc-opt='-I/usr/local/opt/pcre/include -I/usr/local/opt/openssl@1.1/include' --with-ld-opt='-L/usr/local/opt/pcre/lib -L/usr/local/opt/openssl@1.1/lib' --conf-path=/usr/local/etc/nginx/nginx.conf --pid-path=/usr/local/var/run/nginx.pid --lock-path=/usr/local/var/run/nginx.lock --http-client-body-temp-path=/usr/local/var/run/nginx/client_body_temp --http-proxy-temp-path=/usr/local/var/run/nginx/proxy_temp --http-fastcgi-temp-path=/usr/local/var/run/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/var/run/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/var/run/nginx/scgi_temp --http-log-path=/usr/local/var/log/nginx/access.log --error-log-path=/usr/local/var/log/nginx/error.log --with-compat --with-debug --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-ipv6 --with-mail --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module

#Mac 下 Nginx 默认位置

代码语言:javascript复制
/usr/local/etc/nginx/nginx.conf (配置文件路径)
/usr/local/var/www (静态文件路径)
/usr/local/Cellar/nginx/[version] (安装路径?)

TIP

可以直接访问 http://localhost:8080 来访问 Nginx 开启的服务

#打开防火墙端口并重启

代码语言:javascript复制
firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --reload

#查看防火墙打开的所有服务

代码语言:javascript复制
firewall-cmd --list-service

#查看当前打开的端口

代码语言:javascript复制
firewall-cmd --zone=public --list-ports

#打开指定端口

代码语言:javascript复制
firewall-cmd --zone=public --add-port=80/tcp
# (永久生效再加上 --permanent)

TIP

–zone 作用域

–add-port=8080/tcp 添加端口,格式为:端口/通讯协议

–permanent #永久生效,没有此参数重启后失效

#解决 nginx 403 错误问题

第一种情况: 在你的 nginx 配置文件下的 root 字段的路径下没有找到 index.html

第二种情况: 没有用 root 用户运行,导致权限不足,也可以通过将文件夹权限提升再试

#nginx部署前端SPA应用实践

#nginx location 匹配规则

  • ~ 波浪线表示执行一个正则匹配,区分大小写
  • ~* 表示执行一个正则匹配,不区分大小写
  • ^~ 表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
  • = 进行普通字符精确匹配
  • @ 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files

#browserHistory 模式的刷新问题

browserHistory 路由模式下,使用history api可以在前端进行页面跳转,但是刷新的话,就需要对链接进行一个修复(重定向) 可以使用 nginx 的 try_files

代码语言:javascript复制
location / {
        root   /root/deploy/shenyang_military_general_app;
        index  index.html index.htm;
      	try_files $uri $uri/ /index.html;
}

#开启 gzip 压缩

代码语言:javascript复制
gzip  on;
gzip_types    text/plain application/javascript application/x-javascript text/javascript text/xml text/css;

配置gzip_static

代码语言:javascript复制
gzip_static on;

0 人点赞