Nginx
最初想设计成一个HTTP
服务,目标是同时处理10000个连接数。Nginx
内含基于事件的处理机制。
Http
服务器HTTP
缓存Nginx
使用包管理器安装
sudo apt-get instal nginx
验证:curl localhost
从源代码安装
--从官网下载.tar.gz源文件
--创建目录:mkdir $HOME/build
--进入目录并解压:cd $HOME/build && tar xzf nginx-<version-number>.tar.gz
--进入目录并配置:cd $HOME/build/nginx-<version-number> && ./configure
典型邮件代理
./configure --with-mail --with-mail_ssl_module --with-openssl=${BUILD_DIR}/openssl-1.0.0p
配置SSL支持
./configure --with-http_ssl_module --with-openssl=${BUILD_DIR}/openssl-1.0.1p --with-lpenssl-opt=enable-ec_nistp_64_gcc_128
网络加速器
./configure --with-http_ssl_module --with-http_realip_module --with-http_stup_status_module --with-openssl=${BUILD_DIR}/ openssl-1.0.1p
Web服务器
./configure --with-http_stub_status_module
--编译安装:make && sudo make install
.NET Core
应用在Nginx
上运行在Startup.cs
中配置反向代理。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//配置反向代理
app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
服务器配置.NET Core
相关
--确认当前目录:pwd
--创建新目录:mkdir downloads
--进入创建的新目录:cd downloads
--下载Microsoft在Ubuntu 18.04上的Proeuct Key:wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
--安装Microsoft的Product Key:sudo dpkg -i packages-microsoft-prod.deb
--安装.NET Core的运行时:
sudo apt-get update; \
sudo apt-get install -y apt-transport-https && \
sudo apt-get update && \
sudo apt-get install -y aspnetcore-runtime-3.1
发布网站到本地目录。
复制到服务器目录。
配置Nginx
--确认NGinx是否运行:sudo systemctl status nginx
--编辑配置文件:sudo nano /etc/nginx/sites-available/default
server {
listen 80;
server_name example.com *.example.com;
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
--检查配置文件是否正常: sudo nginx -t
--重载nginx: sudo nginx -s reload
--确定当前网站文件所在位置:ls /root/websites/demo
--创建最终网站文件目录:sudo mkdir -p /var/www/publishapplication/demo/
--把源文件拷贝到目标目录:cp -r /root/websites/demo /var/www/publishapplication/ (这里隐含着:/root/webistes/demo目录会拷贝到/publishapplication下的同名目录)
--确认目标目录是否存在:ls /var/www/publishapplication/demo
--创建Service文件:sudo nano /etc/systemd/system/webnix.service
[Unit]
Description=Example My ASP.NET Core Application running on Ubuntu
[Service]
WorkingDirectory=/var/www/publishapplication/publish
ExecStart=/usr/bin/dotnet /var/www/publishapplication/publish/Webnix.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
--启用服务:sudo systemctl enable webnix.service
--查看服务:sudo systemctl status webnix.service
--启动服务:sudo systemctl start webnix.service
外网无法访问
--查看端口:sudo netstat -tunpl|grep 80
--查看进程详情:ps -ef|grep 681
--查看app级别的程序: sudo ufw app list
--查看防火墙的允许:sudo ufw status
--允许:sudo ufw allow 'Nginx HTTP'
--允许:sudo ufw allow 'Nginx HTTPS'
--启用防火墙:sudo ufw enable
--查看防火墙的允许:sudo ufw status
问题解决
最终成功的配置
server {
listen 80;
server_name demo.com *.demo.com;
location / {
root /var/www/html;
index index.html;
try_files $uri $uri/ /index.html;
}
#location / {
# proxy_pass http://localhost:5000;
#proxy_http_version 1.1;
#proxy_set_header Upgrade $http_upgrade;
#proxy_set_header Connection keep-alive;
#proxy_set_header Host $host;
#proxy_cache_bypass $http_upgrade;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header X-Forwarded-Proto $scheme;
#}
}
server {
listen 5006;
root /var/www/html/test;
server_name localhost;
location / {
root /var/www/html/test;
index index.html;
try_files $uri &uri/ /index.html;
}
location /d1 {
alias /var/www/publishapplication/d1.xyz/html;
index index.html;
try_files $uri $uri/ /index.html;
}
}
server {
listen 5007;
server_name api;
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
--确定当前Web目录:/var/www/publishapplication/demo/
--创建目录:
mkdir /var/www/publishapplication/d1
mkdir /var/www/publishapplication/d2
--创建文件:
nano /var/www/publishapplication/d1/index.html
nano /var/www/publishapplication/d2/index.html
--确认目录存在:ls /etc/nginx/conf.d
--创建d1.conf文件:
nano /etc/nginx/conf.d/d1.conf
nano /etc/nginx/conf.d/d2.conf
server {
listen 5007;
server_name d1.xyz;
location / {
root /var/www/publishapplication/d1;
index index.html;
}
}
server {
listen 5008;
server_name d2.xyz;
location / {
root /var/www/publishapplication/d2;
index index.html;
}
}
--确认配置文件是否成功:nginx -t
--重启:sudo systemctl restart nginx
--确认状态:sudo systemctl status nginx
--防火墙允许端口,发现走不通
--编辑配置文件:sudo nano /etc/nginx/sites-available/default
--确认配置是否成功:nginx -t
--重启:sudo systemctl restart nginx
--来到配置目录:cd /etc/nginx
--列出:ls
--进入:cd sites-enabled
--编辑:nano default
--确认nginx的默认页:sudo nano /var/www/html/index.nginx-debian.html
--操作:unlink default
--cd ..
--cd conf.d
--sudo nano new_sites.conf
server {
listen 80 default_server;
server_name demo.com www.demo.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:5006;
}
}
--测试:nginx -t
--sudo systemctl reload nginx
--sudo ufw status
--nano /etc/nginx/sites-available/default
--ls -l /var/www/html
--sudo nano /var/www/html/index.html
-- sudo nano /etc/nginx/sites-available/default
一般
upstream api { //分别监听两个端口应用
server localhost:5000;
server localhost:5001;
}
server { //反向代理的设置在这里
listen 80;
listen [::] 80;
server_name SERVER_IP;
root /home/ryan;
proxy_set_header Host #http_hos;
proxy_set_header X-Real-IP $remote_addr
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / { //路由,根路径会在这里处理
proxy_pass http://api/;
}
}
进入nginx: cd /etc/nginx
浏览配置文件:cat nginx.conf