nginx部署静态网站

一、nginx安装
源码安装 —不考虑 更新时使用
epel源安装 —配置乱 不推荐
官方源安装 —推荐

vim  /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

yum install nginx
二、nginx配置文件分析
cat /etc/nginx/nginx.conf

user  nginx;								# Nginx进程的运行用户身份
worker_processes  1;						# Nginx运行的worker进程数
error_log  /var/log/nginx/error.log warn;	# Nginx错误日志存放的路径
pid        /var/run/nginx.pid;				# Nginx进程运行的PID号

events {
	worker_connections  1024;				#每个worker进程能接受的最大连接数
	use epoll;
}


http {
	include       /etc/nginx/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"';
	
	#访问日志的路径  访问日志的存储格式是main格式, main格式在log_format中进行定义
	access_log  /var/log/nginx/access.log  main;		

	sendfile        on;
	#tcp_nopush     on;
	keepalive_timeout  65;					#超时时间
	#gzip  on;
	include /etc/nginx/conf.d/*.conf;		#包含  *.conf
	
	
	#server<--定义网站
	server {
		listen       80;							#监听80端口
		server_name  localhost;						#网站的域名
		
		location / {								#匹配网站的uri
			root   /usr/share/nginx/html;			#返回资源的具体路径
			index  index.html index.htm;			#返回的具体资源名称
		}
}


http					<---负责请求与响应
	server1				<---定义一个具体的网站
		location 		<---根据用户请求的uri 进行匹配
		location2
		location3
	server2
		location 		<---根据用户请求的uri 进行匹配
		location2
		location3
	server....

三、nginx 运行一个网站
gzip default.conf 压缩,注释
cat default.conf.gz 查看内容

1.vim game.oldxu.com.conf

server {
		listen 80;
		server_name game.oldxu.com;

		location / {
			root /code/;
			index index.html;
		}	
	}

nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

systemctl restart nginx
2.准备好代码的仓库位置
mkdir /code
cd /code/
3.上传代码
git clone https://gitee.com/linex/battlecity.git
mv /battlecity/* ./
4.访问测试
配置hosts劫持
使用火狐、谷歌浏览器
四、如何新增一个网站
1.新增一个nginx的配置文件
[root@web01 code2]# vim /etc/nginx/conf.d/gd.oldxu.com.conf

server {
     listen 80 ;
     server_name gd.oldxu.com;

     location  /  {
     root   /code2;
     index  index.html;
     }
 }

[root@web01 code2]# nginx -t [root@web01 code2]# systemctl restart nginx
2.创建代码存放路径
[root@web01 ~]# mkdir -p /code2 [root@web01 ~]# cd /code2
3.上传游戏
windos rz Mac系统 scp
4配置域名劫持

### 配置Nginx部署静态网站 为了使用Nginx部署静态网站,需要创建并配置相应的站点配置文件。此过程涉及指定服务器监听端口、定义根目录以及处理请求的方式。 #### 创建站点配置文件 假设要托管名为`www.example.com`的静态网站,在Nginx中可以通过编辑位于`sites-available/`下的配置文件来实现这一目标,并通过链接至`sites-enabled/`使该配置生效[^1]: ```nginx server { listen 80; server_name www.example.com; root /var/www/html/static-site; index index.html; location / { try_files $uri $uri/ =404; } } ``` 上述配置说明如下: - `listen 80;`: 表明Web服务器将在HTTP协议默认使用的第80号TCP端口上等待连接。 - `server_name www.example.com;`: 定义了响应哪个域名的请求。 - `root /var/www/html/static-site;`: 设置网页文档所在的绝对路径;这里假定HTML页面存放在`/var/www/html/static-site`下。 - `index index.html;`: 当访问者浏览到根URL时,默认加载`index.html`作为首页。 - `location / {...}`: 对于任何进入根路径的请求,尝试查找对应的物理文件或子目录中的索引页;如果找不到,则返回404错误。 完成以上设置之后,记得重启Nginx服务以便应用更改[^2]。 #### 测试Nginx安装版本 在确保一切正常工作之前,建议先验证当前系统的Nginx版本,这有助于排查可能存在的兼容性问题: ```bash nginx -v ``` 一旦确认无误,即可上传所需的静态资源(如HTML、CSS、JavaScript 文件等),并将它们放置在先前设定好的根目录内。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值