Linux安裝Nginx及配置nginx.conf方式
更新時(shí)間:2024年10月10日 10:43:00 作者:思祺班
本文提供了在Linux系統(tǒng)上安裝Nginx和配置nginx.conf的詳細(xì)步驟,為初學(xué)者提供了便捷的操作指導(dǎo)和個(gè)人經(jīng)驗(yàn)分享,適合需要搭建服務(wù)器的用戶參考
Linux安裝Nginx及配置nginx.conf
操作如下
**一鍵安裝上面四個(gè)依賴** yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel **創(chuàng)建一個(gè)文件夾** cd /usr/local mkdir nginx cd nginx **下載tar包** wget http://nginx.org/download/nginx-1.13.7.tar.gz **解壓tar** tar -xvf nginx-1.13.7.tar.gz **進(jìn)入nginx目錄** cd /usr/local/nginx **進(jìn)入目錄** cd nginx-1.13.7 **執(zhí)行命令 考慮到后續(xù)安裝ssl證書 添加兩個(gè)模塊** ./configure --with-http_stub_status_module --with-http_ssl_module **執(zhí)行make命令** make **執(zhí)行make install命令** make install **啟動(dòng)nginx服務(wù)** /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf **打開配置文件** vi /usr/local/nginx/conf/nginx.conf * *配置nginx.conf在下面 * **配置完后 重啟nginx** /usr/local/nginx/sbin/nginx -s reload **可以看一下Nginx是否啟動(dòng)** ps -ef | grep nginx ###相關(guān)命令 安裝完成一般常用命令 進(jìn)入安裝目錄中, 命令: cd /usr/local/nginx/sbin 啟動(dòng),關(guān)閉,重啟,命令: ./nginx 啟動(dòng) ./nginx -s stop 關(guān)閉 ./nginx -s reload 重啟
注意下面的文字說明
#第一步改這個(gè) 改為root
user root;
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 {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#vue項(xiàng)目的打包后的dist自己放的前端項(xiàng)目位置
root /www/jinghui/dist/;
location /{
try_files $uri $uri/ @router;#需要指向下面的@router否則會(huì)出現(xiàn)vue的路由在nginx中刷新出現(xiàn)404
index index.html index.htm;
}
#對應(yīng)上面的@router,主要原因是路由的路徑資源并不是一個(gè)真實(shí)的路徑,所以無法找到具體的文件
#因此需要rewrite到index.html中,然后交給路由在處理請求資源
location @router {
rewrite ^.*$ /index.html last;
}
# /api :是代理到后端的前綴
location /api {
#填寫直接的內(nèi)網(wǎng)IP地址
proxy_pass http://*.*.*.*:9001/;
#proxy_pass http://&env;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header REMOTE-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
rewrite ^/api/(.*) /$1 break;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
#nacos配置
location /nacos/ {
rewrite ^//(.*)$ /$1 break;
#填寫自己的內(nèi)網(wǎng)IP地址
proxy_pass http://*.*.*.*:8080/nacos;
proxy_set_header Host $host;
proxy_set_header User-Agent $http_user_agent;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header authorization $http_authorization;
}
#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;
}
}
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Linux自動(dòng)化構(gòu)建工具make和Makefile詳解
這篇文章主要介紹了Linux如何自動(dòng)化構(gòu)建工具make和makefile,文章中有詳細(xì)的圖片示例,對學(xué)習(xí)有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2023-04-04
linux adsl 撥號(hào)自動(dòng)配置腳本的方法
本篇文章主要介紹了linux adsl 撥號(hào)自動(dòng)配置腳本的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
Linux系統(tǒng)中SSH服務(wù)基于key認(rèn)證實(shí)踐的過程
這篇文章主要介紹了Linux系統(tǒng)中SSH服務(wù)基于key認(rèn)證實(shí)踐,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
CentOS7設(shè)置jar應(yīng)用程序開機(jī)啟動(dòng)的方法
這篇文章主要介紹了CentOS7設(shè)置jar應(yīng)用程序開機(jī)啟動(dòng)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
Service Temporarily Unavailable的503錯(cuò)誤是怎么回事?
一般來說,出現(xiàn)Service Temporarily Unavailable錯(cuò)誤多半是因?yàn)榫W(wǎng)站訪問量大,造成了流量超限或者并發(fā)數(shù)大引起的資源超限出現(xiàn)的錯(cuò)誤2013-05-05

