一、本机环境。
- centos8
- nginx 1.16.1
二、开始配置。
- 打开配置文件:
vim /etc/nginx/nginx.conf
- 在http块中新建server块:
默认:
http{
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
新建serve块后:
http {
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 8080;
server_name test.com;
root /root/www/test;
index index.html index.htm index.php;
charset utf-8;
access_log logs/test.com.log main;
location / {
}
}
}
以上可以只填listen,server_name,root。
- 配置运行Nginx服务器用户(组)
user root root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
- 保存并退出后重启nginx。
三、常见问题。
- 访问时无响应,请确认访问端口开放。教程
- 访问时返回403,请确认nginx启动用户拥有文件目录的权限。