鼎鼎知识库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

01从创建一个.NET Core网站开始.md 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. # 基本
  2. `Nginx`最初想设计成一个`HTTP`服务,目标是同时处理10000个连接数。`Nginx`内含基于事件的处理机制。
  3. - 反向代理
  4. - 负载均衡
  5. - 动静分离
  6. - web加速器
  7. - 邮件代理
  8. - `Http`服务器
  9. - `HTTP`缓存
  10. # 安装`Nginx`
  11. 使用包管理器安装
  12. ```
  13. sudo apt-get instal nginx
  14. 验证:curl localhost
  15. ```
  16. 从源代码安装
  17. ```
  18. --从官网下载.tar.gz源文件
  19. --创建目录:mkdir $HOME/build
  20. --进入目录并解压:cd $HOME/build && tar xzf nginx-<version-number>.tar.gz
  21. --进入目录并配置:cd $HOME/build/nginx-<version-number> && ./configure
  22. 典型邮件代理
  23. ./configure --with-mail --with-mail_ssl_module --with-openssl=${BUILD_DIR}/openssl-1.0.0p
  24. 配置SSL支持
  25. ./configure --with-http_ssl_module --with-openssl=${BUILD_DIR}/openssl-1.0.1p --with-lpenssl-opt=enable-ec_nistp_64_gcc_128
  26. 网络加速器
  27. ./configure --with-http_ssl_module --with-http_realip_module --with-http_stup_status_module --with-openssl=${BUILD_DIR}/ openssl-1.0.1p
  28. Web服务器
  29. ./configure --with-http_stub_status_module
  30. --编译安装:make && sudo make install
  31. ```
  32. # 让`.NET Core`应用在`Nginx`上运行
  33. 在`Startup.cs`中配置反向代理。
  34. ```
  35. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  36. {
  37. //配置反向代理
  38. app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto});
  39. if (env.IsDevelopment())
  40. {
  41. app.UseDeveloperExceptionPage();
  42. }
  43. else
  44. {
  45. app.UseExceptionHandler("/Home/Error");
  46. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  47. app.UseHsts();
  48. }
  49. app.UseHttpsRedirection();
  50. app.UseStaticFiles();
  51. app.UseRouting();
  52. app.UseAuthorization();
  53. app.UseEndpoints(endpoints =>
  54. {
  55. endpoints.MapControllerRoute(
  56. name: "default",
  57. pattern: "{controller=Home}/{action=Index}/{id?}");
  58. });
  59. }
  60. ```
  61. 服务器配置`.NET Core`相关
  62. ```
  63. --确认当前目录:pwd
  64. --创建新目录:mkdir downloads
  65. --进入创建的新目录:cd downloads
  66. --下载Microsoft在Ubuntu 18.04上的Proeuct Key:wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
  67. --安装Microsoft的Product Key:sudo dpkg -i packages-microsoft-prod.deb
  68. --安装.NET Core的运行时:
  69. sudo apt-get update; \
  70. sudo apt-get install -y apt-transport-https && \
  71. sudo apt-get update && \
  72. sudo apt-get install -y aspnetcore-runtime-3.1
  73. ```
  74. 发布网站到本地目录。
  75. 复制到服务器目录。
  76. 配置`Nginx`
  77. ```
  78. --确认NGinx是否运行:sudo systemctl status nginx
  79. --编辑配置文件:sudo nano /etc/nginx/sites-available/default
  80. server {
  81. listen 80;
  82. server_name example.com *.example.com;
  83. location / {
  84. proxy_pass http://localhost:5000;
  85. proxy_http_version 1.1;
  86. proxy_set_header Upgrade $http_upgrade;
  87. proxy_set_header Connection keep-alive;
  88. proxy_set_header Host $host;
  89. proxy_cache_bypass $http_upgrade;
  90. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  91. proxy_set_header X-Forwarded-Proto $scheme;
  92. }
  93. }
  94. --检查配置文件是否正常: sudo nginx -t
  95. --重载nginx: sudo nginx -s reload
  96. --确定当前网站文件所在位置:ls /root/websites/demo
  97. --创建最终网站文件目录:sudo mkdir -p /var/www/publishapplication/demo/
  98. --把源文件拷贝到目标目录:cp -r /root/websites/demo /var/www/publishapplication/ (这里隐含着:/root/webistes/demo目录会拷贝到/publishapplication下的同名目录)
  99. --确认目标目录是否存在:ls /var/www/publishapplication/demo
  100. --创建Service文件:sudo nano /etc/systemd/system/webnix.service
  101. [Unit]
  102. Description=Example My ASP.NET Core Application running on Ubuntu
  103. [Service]
  104. WorkingDirectory=/var/www/publishapplication/publish
  105. ExecStart=/usr/bin/dotnet /var/www/publishapplication/publish/Webnix.dll
  106. Restart=always
  107. # Restart service after 10 seconds if the dotnet service crashes:
  108. RestartSec=10
  109. KillSignal=SIGINT
  110. SyslogIdentifier=dotnet-example
  111. User=www-data
  112. Environment=ASPNETCORE_ENVIRONMENT=Production
  113. Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
  114. [Install]
  115. WantedBy=multi-user.target
  116. --启用服务:sudo systemctl enable webnix.service
  117. --查看服务:sudo systemctl status webnix.service
  118. --启动服务:sudo systemctl start webnix.service
  119. 外网无法访问
  120. --查看端口:sudo netstat -tunpl|grep 80
  121. --查看进程详情:ps -ef|grep 681
  122. --查看app级别的程序: sudo ufw app list
  123. --查看防火墙的允许:sudo ufw status
  124. --允许:sudo ufw allow 'Nginx HTTP'
  125. --允许:sudo ufw allow 'Nginx HTTPS'
  126. --启用防火墙:sudo ufw enable
  127. --查看防火墙的允许:sudo ufw status
  128. 问题解决
  129. ```
  130. 最终成功的配置
  131. ```
  132. server {
  133. listen 80;
  134. server_name demo.com *.demo.com;
  135. location / {
  136. root /var/www/html;
  137. index index.html;
  138. try_files $uri $uri/ /index.html;
  139. }
  140. #location / {
  141. # proxy_pass http://localhost:5000;
  142. #proxy_http_version 1.1;
  143. #proxy_set_header Upgrade $http_upgrade;
  144. #proxy_set_header Connection keep-alive;
  145. #proxy_set_header Host $host;
  146. #proxy_cache_bypass $http_upgrade;
  147. #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  148. #proxy_set_header X-Forwarded-Proto $scheme;
  149. #}
  150. }
  151. server {
  152. listen 5006;
  153. root /var/www/html/test;
  154. server_name localhost;
  155. location / {
  156. root /var/www/html/test;
  157. index index.html;
  158. try_files $uri &uri/ /index.html;
  159. }
  160. location /d1 {
  161. alias /var/www/publishapplication/d1.xyz/html;
  162. index index.html;
  163. try_files $uri $uri/ /index.html;
  164. }
  165. }
  166. server {
  167. listen 5007;
  168. server_name api;
  169. location / {
  170. proxy_pass http://localhost:5000;
  171. proxy_http_version 1.1;
  172. proxy_set_header Upgrade $http_upgrade;
  173. proxy_set_header Connection keep-alive;
  174. proxy_set_header Host $host;
  175. proxy_cache_bypass $http_upgrade;
  176. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  177. proxy_set_header X-Forwarded-Proto $scheme;
  178. }
  179. }
  180. ```
  181. # 实战配置多个网站一
  182. ```
  183. --确定当前Web目录:/var/www/publishapplication/demo/
  184. --创建目录:
  185. mkdir /var/www/publishapplication/d1
  186. mkdir /var/www/publishapplication/d2
  187. --创建文件:
  188. nano /var/www/publishapplication/d1/index.html
  189. nano /var/www/publishapplication/d2/index.html
  190. --确认目录存在:ls /etc/nginx/conf.d
  191. --创建d1.conf文件:
  192. nano /etc/nginx/conf.d/d1.conf
  193. nano /etc/nginx/conf.d/d2.conf
  194. server {
  195. listen 5007;
  196. server_name d1.xyz;
  197. location / {
  198. root /var/www/publishapplication/d1;
  199. index index.html;
  200. }
  201. }
  202. server {
  203. listen 5008;
  204. server_name d2.xyz;
  205. location / {
  206. root /var/www/publishapplication/d2;
  207. index index.html;
  208. }
  209. }
  210. --确认配置文件是否成功:nginx -t
  211. --重启:sudo systemctl restart nginx
  212. --确认状态:sudo systemctl status nginx
  213. --防火墙允许端口,发现走不通
  214. --编辑配置文件:sudo nano /etc/nginx/sites-available/default
  215. --确认配置是否成功:nginx -t
  216. --重启:sudo systemctl restart nginx
  217. ```
  218. # 实战配置多个网站二(放弃)
  219. ```
  220. --来到配置目录:cd /etc/nginx
  221. --列出:ls
  222. --进入:cd sites-enabled
  223. --编辑:nano default
  224. --确认nginx的默认页:sudo nano /var/www/html/index.nginx-debian.html
  225. --操作:unlink default
  226. --cd ..
  227. --cd conf.d
  228. --sudo nano new_sites.conf
  229. server {
  230. listen 80 default_server;
  231. server_name demo.com www.demo.com;
  232. location / {
  233. proxy_set_header X-Real-IP $remote_addr;
  234. proxy_pass http://localhost:5006;
  235. }
  236. }
  237. --测试:nginx -t
  238. --sudo systemctl reload nginx
  239. --sudo ufw status
  240. ```
  241. # 配置多个静态网站
  242. ```
  243. --nano /etc/nginx/sites-available/default
  244. --ls -l /var/www/html
  245. --sudo nano /var/www/html/index.html
  246. -- sudo nano /etc/nginx/sites-available/default
  247. ```
  248. # 配置文件
  249. 一般
  250. ```
  251. upstream api { //分别监听两个端口应用
  252. server localhost:5000;
  253. server localhost:5001;
  254. }
  255. server { //反向代理的设置在这里
  256. listen 80;
  257. listen [::] 80;
  258. server_name SERVER_IP;
  259. root /home/ryan;
  260. proxy_set_header Host #http_hos;
  261. proxy_set_header X-Real-IP $remote_addr
  262. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  263. location / { //路由,根路径会在这里处理
  264. proxy_pass http://api/;
  265. }
  266. }
  267. ```
  268. ```
  269. 进入nginx: cd /etc/nginx
  270. 浏览配置文件:cat nginx.conf
  271. ```