nginx是最近非常火的web服務器,所以CentOS8也在yum官方源中新增了nginx 1.14,就不用再手動配置源了,直接安裝即可:
yum install -y nginx
稍等一會,即可安裝完成
啓動 nginx
systemctl start nginx.service
如果一切順利的話,現在就可以通過域名或者 ip 訪問了
設置 nginx 開機自啓動
systemctl enable nginx.service
nginx 配置信息
網站文件存放默認位置(Welcome to nginx 頁面)
/usr/share/nginx/html
網站默認站點配置
/etc/nginx/conf.d/default.conf
自定義 nginx 站點配置文件存放目錄
/etc/nginx/conf.d/
nginx 全局配置文件
/etc/nginx/nginx.conf
啓動 nginx
systemctl start nginx.service
關閉 nginx
systemctl stop nginx.service
重啓 nginx
systemctl restart nginx.service
配置
進入 /etc/nginx目錄下,打開 nginx.conf 文件最下面有一句話 include /etc/nginx/conf.d/*.conf; 表明 conf.d 下的 所有以 .conf 結尾的文件都屬於 nginx的配置文件
進入 conf.d 下,只有一個 default.conf 默認配置文件,cp default.conf test.conf 複製一份 default.conf 並改名爲 test.conf
vim test.conf 打開 test.conf (只複製前幾行)
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# ....... 省略中間的代碼
}
第一種配置方法
吧 server_name後的 localhost 改爲自己的域名 比如:``www.baidu.com` 沒有的話,填寫自己的 ip 也行
root 表示 網頁的路徑,改爲自己的 項目的路徑
index 自然就是主頁了,
修改之後的:
server {
listen 80;
server_name www.XXXX.com;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
root /opt/tomcat/apache-tomcat-8.5.39/webapps/wenjuan;
index login.jsp;
}
# ....... 省略中間的代碼
}
find / -name nginx 查找一下名爲nginx的目錄 有一個是 /usr/sbin/nginx,然後進入/usr/sbin,輸入 nginx -t 檢查 nginx配置是否有問題,nginx配置即使有問題,nginx服務也能正常啓動或重啓,只是不按照你的配置工作而已
配置正確的示意圖
錯誤的話會有提示哪個文件第幾行有問題,自行修改即可。
nginx配置正確之後 重啓nginx
systemctl restart nginx
然後瀏覽器訪問你的域名(上面填寫的ip的話,訪問ip就好了)。然後你會發現
我Google了下這個錯誤代碼,造成這個錯誤有兩個原因,1是配置的時候未指定index,2是權限不足(不能訪問你指定的目錄),翻上面看下自己的配置,權限不足的問題
修改方法:打開 /etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
#省略以下代碼
一個簡單的修改方法就是,吧第二行的user之後的 nginx 改爲 root
第二種配置方式
server {
listen 80;
server_name www.junhui.pro;
location / {
proxy_pass http://127.0.0.1:8080/;#代理了服務器8080端口
}
保存之後 在/usr/sbin下,輸入 nginx -t 檢查 nginx配置是否有問題,沒有問題在重啓nginx
如果還有其他什麼問題,可以查看nginx的日誌情況,在 var/log/nginx
下
原文鏈接:https://blog.csdn.net/qq_37638061/article/details/90581358