目錄
一、下載nginx
官網:http://nginx.org/en/download.html
二、用xshell連接服務器
1、創建nginx目錄:mkdir nginx
2、cd nginx
3、配置nginx安裝所需的環境
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
4、打開Xftp傳入壓縮包
5、解壓縮
tar -zxvf nginx-1.19.0.tar.gz
6、進入加壓文件
解壓之後,進入加壓文件,即cd nginx-1.19.0
然後進行配置,推薦使用默認配置,直接./configure
就好了,如下圖所示:
7、編譯安裝nginx
首先在當前目錄(/usr/local/nginx-1.19.0)進行編譯。輸入make即可:
make
然後回車,如果編譯出錯,請檢查是否前面的4個安裝都沒有問題。
編譯成功之後,就可以安裝了,輸入以下指令:
make install
8、啓動nginx
進入/usr/local/nginx/sbin目錄,輸入./nginx即可啓動nginx
./nginx
如果啓動報錯:nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use),說明80端口被佔用,使用如下命令:
fuser -k 80/tcp
關閉nginx
./nginx -s quit 或者 ./nginx -s stop
重啓nginx
./nginx -s reload
查看nginx進程
ps aux|grep nginx
9、設置nginx開機啓動
只需在rc.local增加啓動代碼即可。
vim /etc/rc.local
按鍵盤 i 然後在底部增加:
/usr/local/nginx/sbin/nginx
三、部署
1、打開vue項目,在控制檯輸入
npm run build
2、在xshell進入nginx/html目錄,打開xftp
先刪除原來的index.html,再傳輸
3、把生成的dist目錄下的靜態資源傳輸到服務器
4、修改配置文件
此外,進入cd /usr/local/nginx/conf
目錄可修改nginx的配置文件:
vim nginx.conf
按鍵盤i
進行編輯,編輯完成按 Esc,再輸入 :wq
貼上一個完整版的,其中有序號標明的是註釋說明,主要更改了server 的內容
#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;
server {
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
# 1、8080是需要監聽的端口
listen 8080;
# 2、47.115.5.93是你部署的ip
server_name 47.115.5.93;
# 3、root後面的是前端打包後的傳輸到服務器的路徑
root /usr/local/nginx/html;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root /usr/local/nginx/html/;
# 4、加這一行官方說可以解決刷新後404的問題
try_files $uri $uri/ /index.html;
# 5、這裏配置默認到index.html
index index.html index.htm;
}
# 6、這裏是解決跨域問題,將你後端的地址寫在proxy_pass 後面就可以了
location /api {
rewrite ^.+req/?(.*)$ /$1 break;
proxy_pass http://47.107.158.11/PInn;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 7、如果有多個後端服務器,可繼續添加如下的location
location /req {
rewrite ^.+req/?(.*)$ /$1 break;
proxy_pass http://47.115.12.243;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#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;
# 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
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# 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;
# }
#}
}
5、重啓nginx
在/nginx/sbin/
目錄下
重啓:
./nginx -s reload
查看nginx進程:
ps aux|grep nginx