part1 Nginx Tutorial
Download and Install Nginx
This part refer document
wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar -xzvf nginx-1.24.0.tar.gz
./configure --without-http_rewrite_module
make
make install
After there procedures, nginx is installed in my ubuntu device successfully.
I can use vi /usr/local/nginx/conf/nginx.conf
to modify config file.
Because I have installed apache2 in my machine, so I need to redefined port of nginx
vi /usr/local/nginx/conf/nginx.conf
about line 36-37, change port
and server_name
Here, I change my port
to 12800, and change server_name
to the ipv4 addr of my ECS.
Then, try to start nginx service.
cd /usr/local/nginx/sbin
# Here I add this path to ~/.bashrc additionally, to enable `nginx` command
nginx -s reload
Now, I can access the web page by typing xxxxxx:12800
in explorer.
File structure of Nginx config file
https://www.runoob.com/w3cnote/nginx-setup-intro.html
About Forward Proxy
This part refet to https://www.cnblogs.com/yanjieli/p/15229907.html and https://blog.csdn.net/sleepIII/article/details/100787646
Extra care config here
server {
listen 8080;
server_name localhost;
resolver 114.114.114.114;
proxy_connect;
proxy_connect_allow 443 80;
proxy_connect_connect_timeout 10s;
proxy_connect_read_timeout 10s;
proxy_coneect_send_timeout 10s;
location / {
proxy_pass $scheme://$http_host$request_uri;
}
}
About Reverse Proxy
Firstly, I code a gin service
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "Hello World",
})
})
// 监听并在指定的IPv4地址和端口上启动服务
r.Run("0.0.0.0:12777")
}
这里我没有在ECS的安全组
配置中开放12777端口,由此测试反向代理可以保护内部服务
Direct test
In ECS
root@iZbp180e9tshiea7ipj3khZ:/home/test/from-nginx-to-pingora/helloworld-server# curl http://127.0.0.1:12777/say
[GIN] 2024/03/25 - 14:08:48 | 200 | 32.016µs | 127.0.0.1 | GET "/say"
{"message":"Hello World"}
Local windows
PS C:\Users\scarletborder> curl http://47.98.181.160:12777/say curl : The remote server returned an error: (502) Bad Gateway.
At line:1 char:1
+ curl http://47.98.181.160:12777/say
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Modify nginx.conf
server {
listen 12801;
server_name xxxxxxxx;
#charset koi8-r;
#access_log logs/host.access.log main;
location /say/ {
proxy_pass http://localhost:12777/say;
}
#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;
}
}
Now I can access by type http://xxxxxxx:12801/say/
I can also modify conf file like this
location /to777/ {
proxy_pass http://localhost:12777/;
}
In this condition I can access to it by access xxxx:12801/to777/say
跨域问题
例如前端在8080上开放index.html而后端运行在12777端口号上,虽然能成功发送但是脚本无法获得相应主体
现在想都放到12801下实现同域
server{
listen 12801;
server_name xxxxxxx;
# 配置代理服务器
location /backend/say/{
proxy_pass http://localhost:12777/;
}
# 配置前端服务器
location /web/{
proxy_pass http://localhost:12800/;
}
server{
listen 12800;
server_name xxxxxxxx;
location /{
root html;
index myindex.html;
}
}
}
负载均衡
每台slave具有相同的功能,这里用不同的端口号表示在不同服务器上的项目
localhost:12777
and localhost:12778
创建ano_helloworld-server
directory,但是会返回hello another world
这里配置文件的路径
/http/server
/http/upstream
配置负载均衡conf/nginx.conf
http{
upstream mybind{
server localhost:12777 max_fails=1 weight=1 fail_timeout=10s;
server localhost:12778 max_fails=1 weight=1 fail_timeout=10s;
#weight: 权重,设置相同时则是轮询访问,也可以设置不同的权重,则按对应的比例进行访问
#max_fails: 最大请求失败次数,默认值为1
#fail_timeout: 请求失败超时时间,在经历了max_fails次失败后,暂停服务的时间。
}
server{
listen 12802;
server_name xxxx;
location / {
# 直接填集群的名称,需要和上文保持一致
proxy_pass http://mybind;
}
}
}
Then I can access http://xxxxxxx:12802/say
to access my assets.
0 评论:
发表评论