1. 相关的命令
- 查看服务器是否安装nginx
whereis nginx
- 安装nginx
yum install -y nginx
- 切换到nginx文件中
cd /etc/nginx
ll // 查看当前文件夹的文件
- 查看nginx.conf文件
vim nginx.conf
按 i 编辑文件
按 esc 接着输入 :wq 保存文件并退出
- 替换nginx.conf文件
cd /etc/nginx
rm -rf nginx.conf // 删除当前nginx.conf配置文件
cp -R /tmp/nginx.conf . // 复制nginx.conf到当前文件夹(保证tmp文件夹中有nginx.conf文件)
- 启动nginx
nginx 或
sudo systemctl start nginx
- 查看nginx状态
systemctl status nginx
- 查看端口是否被占用
sudo lsof -i :8848
- 杀死当前占用的端口
kill -9 XXXXX(pid)
- 打开对于某个端口的防火墙
// 执行以下命令以永久性的开发82端口
sudo firewall-cmd --zone=public --add-port=82/tcp --permanent
// 重新加载防火墙规则
sudo firewall-cmd --reload
// 验证端口是否已开放
sudo firewall-cmd --list-all
- 终止nginx
nginx -s stop
- 重启nginx
nginx -s reload
2. nginx.conf文件分析
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 8848;
# listen [::]:80;
server_name _;
root /home/web/dist;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
#Nginx用户及组:用户 组。window下不指定
#user nobody;
#工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU。
worker_processes 1;
#错误日志:存放路径。
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid(进程标识符):存放路径
pid /usr/local/nginx/logs/nginx.pid;
#一个进程能打开的文件描述符最大值,理论上该值因该是最多能打开的文件数除以进程数。
#但是由于nginx负载并不是完全均衡的,所以这个值最好等于最多能打开的文件数。
#LINUX系统可以执行 sysctl -a | grep fs.file 可以看到linux文件描述符。
worker_rlimit_nofile 65535;
events {
#使用epoll的I/O 模型。linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
use epoll;
#单个进程最大连接数(最大连接数=连接数*进程数)
worker_connections 1024;
#客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,
#一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。
#client_header_buffer_size 4k;
}
http {
#设定mime类型,类型由mime.type文件定义
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"';
#用了log_format指令设置了日志格式之后,需要用access_log指令指定日志文件的存放路径
#记录了哪些用户,哪些页面以及用户浏览器、ip和其他的访问信息
#access_log logs/host.access.log main;
#access_log logs/host.access.404.log log404;
#服务器名字的hash表大小
server_names_hash_bucket_size 128;
#客户端请求头缓冲大小。
#nginx默认会用client_header_buffer_size这个buffer来读取header值,
#如果header过大,它会使用large_client_header_buffers来读取。
#如果设置过小HTTP头/Cookie过大 会报400 错误 nginx 400 bad request
#如果超过buffer,就会报HTTP 414错误(URI Too Long)
#nginx接受最长的HTTP头部大小必须比其中一个buffer大
#否则就会报400的HTTP错误(Bad Request)
#client_header_buffer_size 32k;
#large_client_header_buffers 4 32k;
#隐藏ngnix版本号
#server_tokens off;
#忽略不合法的请求头
#ignore_invalid_headers on;
#让 nginx 在处理自己内部重定向时不默认使用 server_name设置中的第一个域名
#server_name_in_redirect off;
#客户端请求体的大小
#client_body_buffer_size 8m;
#开启文件传输,一般应用都应设置为on;若是有下载的应用,则可以设置成off来平衡网络I/O和磁盘的I/O来降低系统负载
sendfile on;
#告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送。
#tcp_nopush on;
#tcp_nodelay off 会增加通信的延时,但是会提高带宽利用率。在高延时、数据量大的通信场景中应该会有不错的效果
#tcp_nodelay on,会增加小包的数量,但是可以提高响应速度。在及时性高的通信场景中应该会有不错的效果
tcp_nodelay on;
#长连接超时时间,单位是秒
keepalive_timeout 65;
#gzip模块设置,使用 gzip 压缩可以降低网站带宽消耗,同时提升访问速度。
#gzip on; #开启gzip
#gzip_min_length 1k; #最小压缩大小
#gzip_buffers 4 16k; #压缩缓冲区
#gzip_http_version 1.0; #压缩版本
#gzip_comp_level 2; #压缩等级
#gzip_types text/plain text/css text/xml text/javascript application/json application/x-javascript application/xml application/xml+rss;#压缩类型
#负载均衡
#max_fails为允许请求失败的次数,默认为1
#weight为轮询权重,根据不同的权重分配可以用来平衡服务器的访问率。
# upstream myServer{
# server 192.168.247.129:8080 max_fails=3 weight=2;
# server 192.168.247.129:8081 max_fails=3 weight=4;
# }
#server {
# listen 80;
#
# #IP/域名可以有多个,用空格隔开
# server_name 192.168.247.129;
# #server_name www.test.com;
#
# #charset koi8-r;
#
# #access_log logs/host.access.log main;
#
# #反向代理配置,
# #将所有请求为www.test.com的请求全部转发到upstream中定义的目标服务器中。
# location / {
#
# #此处配置的域名必须与upstream的域名一致,才能转发。
# proxy_pass http://myServer;
# #proxy_pass http://192.168.247.129:8080;
#
# proxy_connect_timeout 20; #nginx跟后端服务器连接超时时间(代理连接超时)
#
# #client_max_body_size 10m; #允许客户端请求的最大单文件字节数
# #client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
# #proxy_send_timeout 300; #后端服务器数据回传时间(代理发送超时)
# #proxy_read_timeout 300; #连接成功后,后端服务器响应时间(代理接收超时)
# #proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
# #proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
# #proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
# #proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
#
# root html;
#
# #定义首页索引文件的名称
# index index.html index.htm;
# }
#
# #动静分离 静态资源走linux 动态资源走tomcat
# # 注意 /source/image/下面寻找资源
# location /image/ {
# root /source/;
# autoindex on;
# }
#
#
# # 出现50x错误时,使用/50x.html页返回给客户端
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# root html;
# }
#}
#下面是配置生产环境中既支持HTTP又支持HTTPS,保证用户在浏览器中输入HTTP也能正常访问
# SSL证书 配置
ssl_certificate cert/yphtoy.com.pem; #加密证书路径
ssl_certificate_key cert/yphtoy.com.key; #加密私钥路径
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #加密协议
ssl_session_cache shared:SSL:1m; #加密访问缓存设置,可以大大提高访问速度
ssl_session_timeout 10m; #加密访问缓存过期时间
ssl_ciphers HIGH:!aNULL:!MD5; #加密算法
ssl_prefer_server_ciphers on; #是否由服务器决定采用哪种加密算法
# 负载均衡
upstream api_upstream
{
server 127.0.0.1:8080 max_fails=3 weight=1;
server 127.0.0.1:8081 max_fails=3 weight=1;
}
#api 接口(兼容HTTP)
server{
listen 80;
server_name api.test.com;
# 301重定向跳转到HTTPS接口
return 301 https://$server_name$request_uri;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#api 接口(兼容HTTPS)
server{
listen 443 ssl;
server_name api.test.com;
location / {
root html;
index index.html index.htm;
proxy_pass http://api_upstream;
#语法: proxy_cookie_path oldpath replacepath;
#oldpath就是你要替换的路径 replacepath 就是要替换的值
#作用:同一个web服务器下面多个应用之间能获取到cookie
proxy_cookie_path /api/ /;
#服务端接收的请求头Cooke值不变
proxy_set_header Cookie $http_cookie;
}
}
#管理后台端(兼容HTTP)
server{
listen 80;
server_name manage.test.com;
# 301重定向跳转到HTTPS接口
return 301 https://$server_name/$request_uri;
error_page 500 502 503 504 /50x.html;
location = /50x.html{
root html
}
}
#管理后台端(兼容HTTPS)
server{
listen 443 ssl;
server_name manage.test.com;
location / {
root /home/test/web/dist
index /index.html;
#语法:try_files 【$uri】 【 $uri/】 【参数】
#当用户请求https://manage.test.com/login时,
#一.如果配置了上面的默认index,会依次请求
#1./home/test/web/dist/login 查找有没有login这个文件,没有的话
#2./home/test/web/dist/index.html 有就直接返回
#二.如果没有配置了上面的默认index或者配置了没有找到对应的资源,会依次请求
#1./home/test/web/dist/login 查找有没有login这个文件,没有的话
#2./home/test/web/dist/login/ 查找有没有login这个目录,没有的话
#3.请求https://manage.test.com/index.html nginx内部做了一个子请求
#三.总的来说,index的优先级比try_files高,请求会先去找index配置,这里最后一个参数必须存在
try_files $uri $uri/ /index.html;
#解决跨域问题
#允许跨域请求地址(*表示全部,但是无法满足带cookie请求,因为cookie只能在当前域请求)
add_header Access-Control-Allow-Origin $http_origin;
#允许接收cookie和发送cookie
add_header Access-Control-Allow-Credentials 'true';
#允许请求的方法
add_header Access-Control-Allow-Methods 'GET,POST,DELETE,PUT,OPTIONS';
#允许请求头(Content-Type:请求数据/媒体类型 x-requested-with:判断请求是异步还是同步 自定义header 比如 token)
add_header Access-Control-Allow-Headers $http_access_control_request_headers;
#浏览器缓存请求头信息,1800秒内,只会有1次请求,不会出现"OPTIONS"预请求,节约资源
#add_header Access-Control-Max-Age '1800';
if ($request_method = 'OPTIONS') {
return 204;
}
#服务端HttpServletRequest可以获得用户的真实ip
proxy_set_header X-Real-IP $remote_addr;
#服务端HttpServletRequest可以获得用户的真实ip和经过的每一层代理服务器的ip
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#服务端接收的请求头Host值不变
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
}
}
}