本地调试

nginx 如何配置

  1. # 本地调试模拟域名
  2. server{
  3. # gzip用来压缩前端文件体积
  4. gzip on;
  5. gzip_min_length 1024;
  6. gzip_buffers 4 16k;
  7. gzip_comp_level 9;
  8. gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/javascript;
  9. gzip_vary on;
  10. # 上传文件大小
  11. client_max_body_size 5m;
  12. listen 80;
  13. # 输入您的域名 t.fashop.cn 只为演示代码,如果您没有域名可以在本地的host里进行模拟,加一行 127.0.0.1 t.fashop.cn,这样就拦截了这个域名跳到本地项目,请尽可能的模拟线上环境
  14. server_name t.fashop.cn;
  15. fastcgi_connect_timeout 300;
  16. fastcgi_send_timeout 300;
  17. fastcgi_read_timeout 300;
  18. # 映射到swoole端口
  19. location / {
  20. proxy_pass http://127.0.0.1:9510;
  21. proxy_redirect off;
  22. proxy_set_header Host $host;
  23. proxy_set_header X-Real-IP $remote_addr;
  24. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  25. proxy_http_version 1.1;
  26. proxy_set_header Upgrade $http_upgrade;
  27. proxy_set_header Connection "upgrade";
  28. proxy_connect_timeout 300s;
  29. proxy_send_timeout 300s;
  30. proxy_read_timeout 300s;
  31. }
  32. # 上传资源
  33. location ~/Upload/.*\.(gif|jpg|jpeg|png|txt|mp3|mp4|rmvb|mkv|avi|mov|MOV|json|ico|css|js)$ {
  34. root /var/www/fashop/;
  35. }
  36. # 前端项目资源匹配
  37. location ~/manifest.json$ {
  38. root /var/www/fashop/a/;
  39. }
  40. # 前端项目资源匹配
  41. location ~/favicon.ico$ {
  42. root /var/www/fashop/a/;
  43. }
  44. # 前端项目资源匹配
  45. location ~/logo.png {
  46. root /var/www/fashop/a/;
  47. }
  48. # 前端项目资源匹配
  49. location ~/logo-black.png {
  50. root /var/www/fashop/a/;
  51. }
  52. # 前端项目资源匹配
  53. location ~/static/.*\.(gif|jpg|jpeg|png|txt|mp3|mp4|rmvb|mkv|avi|mov|MOV|json|ico|css|js)$ {
  54. root /var/www/fashop/a/;
  55. }
  56. # 前端项目资源匹配
  57. location ~(/a$|/a/) {
  58. root /var/www/fashop/;
  59. try_files $uri /a/index.html;
  60. }
  61. }

你可能会遇到的问题:

1.如果nginx也配置完了,当访问你的服务器或者虚拟机时出现无法访问,请检查你的nginx是否开启:

  1. [注释:查询nginx状态]
  2. [root@fashop fashop] service nginx status
  3. [注释:启动nginx]
  4. [root@fashop fashop] service nginx start
  5. [注释:重新启动nginx]
  6. [root@fashop fashop] service nginx restart
  7. [注释:停止nginx]
  8. [root@fashop fashop] service nginx stop
  9. [假如你不想每次开机都启动一下nginx,可设置nginx开机自动启动]
  10. [root@fashop fashop] chkconfig nginx on
  11. [同理,假如你不想每次开机都启动一下php-fpm,可设置php-fpm开机自动启动]
  12. [root@fashop fashop] chkconfig php-fpm on

2.假如你的nginx配置好并开启了,但是你访问你的fashop还是无法访问,那么请检查你的linux的防火墙是否关闭:

  1. [查看防火墙状态]
  2. [root@fashop fashop] firewall-cmd --state
  3. [关闭防火墙]
  4. [root@fashop fashop] systemctl stop firewalld.service
  5. [开启防火墙]
  6. [root@fashop fashop] systemctl start firewalld.service
  7. [禁止开机启动启动防火墙]
  8. [root@fashop fashop] systemctl disable firewalld.service

3.nginx和防火墙都配置好了,但是你发现 http://xxx.xxx.xxx.xxx:9510/install 可以访问安装目录, http://xxx.xxx.xxx.xxx/install 不能访问,使用Nginx端口转发出现502错误,那么可能是 SELinux 问题:

什么是SELinux?SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统。

解决办法: 关闭SELiux。

  1. [查看selinux状态]
  2. [root@fashop fashop] sestatus
  3. SELinux status: enabled
  4. SELinuxfs mount: /sys/fs/selinux
  5. SELinux root directory: /etc/selinux
  6. Loaded policy name: targeted
  7. Current mode: enforcing
  8. Mode from config file: enforcing
  9. Policy MLS status: enabled
  10. Policy deny_unknown status: allowed
  11. Max kernel policy version: 2
  12. [临时关闭selinux]
  13. [root@fashop fashop] setenforce 0
  14. [永久关闭selinux:将SELINUX=enforcing改为SELINUX=disabled 设置后需要重启才能生效]
  15. [root@fashop fashop] vi /etc/selinux/config