Linux安装nginx
sudo apt-get install build-essential
sudo apt-get install libtool
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install libpcrecpp0v5 libssl-dev gcc
sudo apt-get install zlib1g zlib1g-dev
sudo apt-get install openssl openssl-dev
2、下载nginx
下载地址1:http://nginx.org/en/download.html
下载地址2:wget http://nginx.org/download/nginx-1.13.6.tar.gz
解压压缩包:tar -zxvf nginx-1.13.6.tar.gz
进入目录:cd nginx-1.13.6
配置产出路径: ./configure --prefix=/opt/softWare/nginx
配置产出路径带ssl: ./configure --prefix=/opt/softWare/nginx --with-http_ssl_module
编译:make
安装:sudo make install
临时启动:sudo /opt/softWare/nginx/sbin/nginx -c /opt/softWare/nginx/conf/nginx.conf
创建软连接方便使用:sudo ln -s /opt/softWare/nginx/sbin/nginx /usr/bin/nginx
ps -ef | grep nginx
添加ssl模块
编译: ./configure --prefix=/opt/softWare/nginx --with-http_ssl_module
configure arguments: –with-http_ssl_module说明ssl模块已安装
三、配置文件
3.1编译方式安装的nginx
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
log_format main '{"remote_addr":"$remote_addr","remote_user":"$remote_user","time_local":"$time_local","reque
st":"$request","sta
tus":"$status","request_time":"$request_time","upstream_response_time":"$upstream_response_time","request_length":"
$request_length
","bytes_sent":"$bytes_sent","body_bytes_sent":"$body_bytes_sent","gzip_ratio":"$gzip_ratio","http_referer":"$http_
referer","http_user_agent":"$http_user_agent"}';
access_log logs/access.log main;
error_log logs/error.log;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
gzip on;
#大于1kb才压缩
gzip_min_length 1k;
gzip_buffers 4 16k;
#压缩级别1-10 越大压缩越费时间越好
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php
image/jpeg image/gif image/png;
gzip_vary off;
#去除ie6以下支持
gzip_disable "MSIE [1-6]\.";
include /opt/softWare/nginx/conf/conf.d/*.conf;
}
3.1 apt 方式安装的nginx
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 1024;
# multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /opt/softWare/nginx/conf/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
log_format main '{"remote_addr":"$remote_addr","remote_user":"$remote_user","time_local":"$time_local","reques
t":"$request","status":"$status","request_time":"$request_time","request_length":"$request_length","bytes_sent":"$bytes_sent","body_bytes_sent":"$body_bytes_sent","gzip_rati
o":"$gzip_ratio","connection_requests":"$connection_requests","http_referer":"$http_referer","http_user_agent":"
$http_user_agent","http_x_forwarded_for":"$http_x_forwarded_for"}';
access_log /opt/softWare/nginx/log/access.log main;
error_log /opt/softWare/nginx/log/error.log;
#开启图片和html压缩
gzip on;
#大于1kb才压缩
gzip_min_length 1k;
gzip_buffers 4 16k;
#压缩级别1-10 越大压缩越费时间越好
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-ht
tpd-php image/jpeg image/gif image/png;
gzip_vary off;
#去除ie6以下支持
gzip_disable "MSIE [1-6]\.";
include /opt/softWare/nginx/conf/conf.d/*.conf;
}
3.2 conf.d目录下robots2.com文件
server {
listen 443 ssl;
server_name www.robots2.com;
ssl on;#新版本去除并443后面加ssl
ssl_certificate /opt/softWare/nginx/cert/5571550_www.robots2.com.pem;
ssl_certificate_key /opt/softWare/nginx/cert/5571550_www.robots2.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
# 上述端口指向的根目录
root /opt/project/catering_earth/litemall-admin/dist;
# 项目根目录中指向项目首页
index index.html;
# 根请求会指向的页面
location / {
# 此处的 @router 实际上是引用下面的转发,否则在 Vue 路由刷新时可能会抛出 404
try_files $uri $uri/ @router;
# 请求指向的首页
index index.html;
}
# 由于路由的资源不一定是真实的路径,无法找到具体文件
# 所以需要将请求重写到 index.html 中,然后交给真正的 Vue 路由处理请求资源
location @router {
rewrite ^.*$ /index.html last;
}
location /wx/ {
proxy_pass http://localhost:8082;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /admin/ {
proxy_pass http://localhost:8083;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
# 上述端口指向的根目录
root /opt/nowProject/catering_earth/litemall-admin/dist;
# 项目根目录中指向项目首页
index index.html;
client_max_body_size 20m;
client_body_buffer_size 128k;
# 根请求会指向的页面
location / {
# 此处的 @router 实际上是引用下面的转发,否则在 Vue 路由刷新时可能会抛出 404
try_files $uri $uri/ @router;
# 请求指向的首页
index index.html;
}
# 由于路由的资源不一定是真实的路径,无法找到具体文件
# 所以需要将请求重写到 index.html 中,然后交给真正的 Vue 路由处理请求资源
location @router {
rewrite ^.*$ /index.html last;
}
}