目录
Ⅰ.使用 yum-config-manager 命令添加 OpenResty 官方源
3.在nginx的配置文件中定义好端口,以及编写lua代码(也可以写脚本放进去,后面演示)
七、vue项目对nginx进行发送异步请求操作的时候跨域问题的解决
一、安装Openresty
1.预编译安装
预编译安装是默认安装在/usr/local/openresty ,而且得到的是最新版的openresty
Ⅰ.使用 yum-config-manager
命令添加 OpenResty 官方源
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
中间会问几次问题 输入 y继续(截图下次我配置服务器的时候补齐)
Ⅱ.执行以下命令来安装 OpenResty:
yum install openresty
安装到的路径是/usr/local/openresty
2.利用包安装
我用的第一种方法,包好像重复上传了,我还是推荐第一种方法安装,快捷方便
二、配置内置的nginx,并自定义输出lua语句
1. 修改/etc/profile,配置nginx环境变量
我用的图形化工具,不是图形化工具用FinalShell也差不多,
cd /etc
不是图形化工具还不用FinalShell就只能慢慢改了(下面是打开文件指令,用图形化直接双击可以编辑)
vi profile
翻到配置的最下面是定义环境变量的
将
export PATH=$PATH:/usr/local/openresty/nginx/sbin
加入
其中 export PATH
的作用是将当前的 PATH
变量导出,使其对其他进程和子进程可见。这样做是为了确保新添加的路径在整个系统中生效。
有时候你会看到别人可能写的
export PATH=/usr/local/openresty/nginx/sbin:$PATH
当你使用 export PATH=$PATH:/usr/local/openresty/nginx/sbin
时,$PATH
表示先获取当前的 PATH
值,然后将 /usr/local/openresty/nginx/sbin
添加到 PATH
值的末尾。这样做可以保留原有的 PATH
设置并扩展它。
当你使用 export PATH=/usr/local/openresty/nginx/sbin:$PATH
时,$PATH
表示将当前的 PATH
值添加到 /usr/local/openresty/nginx/sbin
的前面。这样做可以将特定路径添加到 PATH
的开头。
2. 使profile生效后用浏览器测试启动情况
之前忘记写启动语句了十分抱歉
启动就是
nginx
刷新配置文件,确保刚才环境变量修改成功
source /etc/profile
不会输出什么,是我不小心按到ctrl+c才这样
验证启动没有
ps -ef | grep nginx
nginx: master process
(进程号为 90372)是 Nginx 的主进程。
nginx: worker process
(进程号为 90373)是 Nginx 的工作进程。
有两个 Nginx 进程正在运行。主进程负责管理工作进程,而工作进程实际处理来自客户端的请求。
请注意,每个 Nginx 工作进程都可以处理多个并发连接。Nginx 使用多进程或多线程的方式,可以同时处理多个请求,以提高性能和吞吐量。
我们去网页看看 我用的是虚拟机,所以安装的全套,有浏览器的(没有浏览器就直接测试第
二步就行了,这个操作只是试一下虚拟机访问虚拟机本身和我们window访问虚拟机)
Ⅰ、虚拟机/服务器 浏览器测试
http://localhost
因为我们的是OpenResty的nainx所以启动之后是
Ⅱ、主机测试
直接访问 服务器地址,或者虚拟机地址 ,比如我虚拟机ip 192.168.189.101 如下就是成功(不存在就是提示xxx响应超时)
-- 你的服务器ip地址,或者虚拟机ip地址
192.168.189.101
3.在nginx的配置文件中定义好端口,以及编写lua代码(也可以写脚本放进去,后面演示)
cd /usr/local/openresty/nginx/conf
vi nginx.conf
这次先不演示纯用指令怎么搞了,即使没有ssh可视化界面用,用FinalShell也差不多(后面出文具体将怎么写,平时演示指令只是为了防止遗忘,但是这种修改配置的,个人还是建议在可视化界面修改,不然小问题找半天,平时还是用其他的训练操作,别拿配置文件训练。)
刚配置好应该只有这一个server新增一个server
listen是端口号,尽量写的奇怪点,不要想当然,比如8848是被nacos用的,不确定的时候可以查一下也没有被用
当有多个server的时候,不输入端口号的话默认是访问80端口
重启nginx
nginx -s reload
有时候改了nginx.conf,nginx变量 又失效了
记得重新加载一下环境变量(然后再用上一步重启)
source /etc/profile
如果启动失败的话,可以先用我的配置,我启动的时候也出了些问题,后面注释了一部分地方解决了,不然进行不了下一步
#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;
server {
listen 1541;
location / {
default_type text/html;
content_by_lua_block {
ngx.say("<p>hello, world</p>")
}
}
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
#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;
# }
# 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
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# 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;
# }
#}
3.接下来访问一下看看效果
http://127.0.0.1:1541
http://192.168.189.101:1541
第一个是在虚拟机或者服务器自带的浏览器里面测试,第二个就直接在本机上面
当然,第二个你运行不了,要开放端口(你防火墙关了当我没说,服务器还要更改安全组。猜猜前面为什么能访问,因为80端口在防火墙和服务器安全组都是默认开放的