參考Centos7安裝Nginx實(shí)戰(zhàn),寫的非常詳細(xì)。
1.查看Nginx所有版本:http://nginx.org/download/
2.我選用的是nginx-1.18.0.tar.gz 這個(gè)包
3.Nginx基本使用方式:
(1).進(jìn)入安裝目錄
cd /usr/local/nginx/sbin
(2).啟動(dòng)
./nginx
(3).關(guān)閉 nginx
./nginx -s stop
(4).重啟 nginx
./nginx -s reload
(5).Nginx在啟動(dòng)時(shí)默認(rèn)加載conf/nginx.conf文件,這個(gè)文件就是nginx的配置文件了救欧,我給出我的配置:
cd /usr/local/nginx/conf
vim nginx.conf
#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;
# 這里監(jiān)聽 80端口终议,默認(rèn)打開前臺(tái)頁面
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/blog/front/web/dist/; # 這個(gè)是我前臺(tái)前端打包后的文件放置的位置铭污,根據(jù)自己的修改七婴。
index index.html index.htm; # 加載dist目錄下的index.html頁面
try_files $uri $uri/ @router;
autoindex on;
}
location /api/ {
# 這里是接口代理 比如你前臺(tái)頁面接口請(qǐng)求地址為:/api/login,那么會(huì)代理成http://127.0.0.1:7001/api/login官还。注意:proxy_pass 配置的http://127.0.0.1:7001后面沒有/饶氏。
proxy_pass http://127.0.0.1:7001; # 這個(gè)地址是我后臺(tái)服務(wù)啟動(dòng)后的地址
proxy_set_header Host $host:$server_port;
}
location @router{
# 這里配置是因?yàn)槲覀兊捻?xiàng)目是Vue或React通過js渲染出來的讥耗。默認(rèn)打包的只要index.html。所有需要做如下的路由配置疹启。
rewrite ^.*$ /index.html last;
}
gzip on;
gzip_buffers 32 4k;
gzip_comp_level 6;
gzip_min_length 200;
gzip_types text/css text/xml application/javascript;
gzip_vary on;
#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
# 管理后臺(tái)服務(wù)代理
server {
listen 9000;
server_name localhost;
location / {
root /usr/blog/front/admin/dist/;
index index.html index.htm;
try_files $uri $uri/ @router;
autoindex on;
}
location /api/v1/ {
proxy_pass http://127.0.0.1:7001;
proxy_set_header Host $host:$server_port;
}
location @router{
rewrite ^.*$ /index.html last;
}
gzip on;
gzip_buffers 32 4k;
gzip_comp_level 6;
gzip_min_length 200;
gzip_types text/css text/xml application/javascript;
gzip_vary on;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# 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;
# }
#}
}
我是開了兩個(gè)代理的:
前臺(tái)展示打開的服務(wù)代理和管理后臺(tái)打開的服務(wù)代理古程,這個(gè)項(xiàng)目是分開端口訪問的。 比如:我的公網(wǎng)IP是 47.134.30.148喊崖,那么可以通過 http://47.134.30.148 即可訪問前臺(tái)展示挣磨,http://47.134.30.148:9000 即可訪問管理后臺(tái)的登錄界面。
至于為什么要做如下配置:參考react,vue等部署單頁面項(xiàng)目時(shí),訪問刷新出現(xiàn)404問題
try_files $uri $uri/ @router;
location @router{
rewrite ^.*$ /index.html last;
}
建議仔細(xì)閱讀我上面提到的參考文章荤懂。里面的講解都是非常詳細(xì)的茁裙。