MAC Nginx配置:每次电脑重启之后,nginx宕机无法完成前后端代理转发

252 阅读2分钟

不能打开本地页面的话,重新安装nginx可以解决,已经三次了!
要先kill -9 进程号,卸载brew uninstall nginx,然后再安装,再sudo nginx,然后根据提示,创建一个文件夹和一个文件就能用了。

不用重新装,这次先ps -ef|grep nginx接下来kill -9 进程号(第二列),然后lsof -i:xxx 确保进程杀掉,最后sudo nginx

a.上面博客说了在Linux中安装nginx。博文地址为:www.cnblogs.com/hanyinglong…

  b.当Nginx安装完毕后,会有相应的安装目录,安装目录里的nginx.confg为nginx的主配置文件,nginx主配置文件分为4部分,main(全局配置)、server(主机配置)、upstream(负载均衡服务器设置)以及location(URL匹配特定位置的设置),这四者的关系是:server继承main,location继承server,upstream既不会继承其它设置也不会被继承。

  c.Nginx是一个代理服务器,一般情况下,网站是不能部署在Nginx下的,比如用Java开发的JavaWeb程序,我们部署在tomcat下,然后使用Nginx代理将网址指向tomcat即可。

3.Nginx代理网站

a.我在tomcat下部署了一个javaweb项目,tomcat安装的服务器IP为:192.168.37.136,部署的项目在tomcat下的访问地址为:http://192.168.37.136:8080/lywh/

  b.我在IP为192.168.37.133的服务器下面安装成功了Nginx。

  c.那怎么样将tomcat下部署的网站使用Nginx代理呢?,修改Nginx的配置文件,修改命令:vim /usr/local/nginx/conf/nginx.conf

1 #user nobody; 2 worker_processes 1; 3 #error_log logs/error.log; 4 #error_log logs/error.log notice; 5 #error_log logs/error.log info; 7 #pid logs/nginx.pid; 10 events { 11 worker_connections 1024; 12 } 15 http { 16 include mime.types; 17 default_type application/octet-stream; 18 19 #log_format main 'remoteaddrremote_addr - remote_user [timelocal]"time_local] "request" ' 20 # 'statusstatus body_bytes_sent "http_referer" ' 21 # '"http_user_agent" "http_x_forwarded_for"'; 22 23 #access_log logs/access.log main; 24 25 sendfile on; 26 #tcp_nopush on; 27 28 #keepalive_timeout 0; 29 keepalive_timeout 65; 30 31 #gzip on; 32 33 #配置tomcat的IP地址和访问端口 34 upstream gw { 35 server 192.168.37.136:8080 weight=1; 36 } 37 server { 38 listen 80; 39 server_name localhost; 40 41 #charset koi8-r; 42 43 #access_log logs/host.access.log main; 44 45 location / { 46 root html; 47 index index.html index.htm; 48 } 49 #Nginx代理配置 50 location /lywh { 51 proxy_pass http://gw/lywh; 52 } 53 location /sapi { 54 proxy_pass http://gw/shopappapi; 55 } 56 location /cas{ 57 proxy_pass http://gw/cas-server-webapp-4.0.0/login; 58 } 59 location /doc{ 60 proxy_pass http://gw/docs; 61 } 62 63 #error_page 404 /404.html; 64 65 # redirect server error pages to the static page /50x.html 66 # 67 error_page 500 502 503 504 /50x.html; 68 location = /50x.html { 69 root html; 70 } 71 72 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 73 # 74 #location ~ \.php { 75 # proxy_pass http://127.0.0.1; 76 #} 77 78 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 79 # 80 #location ~ .php { 81 # root html; 82 # fastcgi_pass 127.0.0.1:9000; 83 # fastcgi_index index.php; 84 # fastcgi_param SCRIPT_FILENAME /scriptsfastcgi_script_name; 85 # include fastcgi_params; 86 #} 87 88 # deny access to .htaccess files, if Apache's document root 89 # concurs with nginx's one 90 # 91 #location ~ /.ht { 92 # deny all; 93 #} 94 } 95 96 97 # another virtual host using mix of IP-, name-, and port-based configuration 98 # 99 #server { 100 # listen 8000; 101 # listen somename:8080; 102 # server_name somename alias another.alias; 103 104 # location / { 105 # root html; 106 # index index.html index.htm; 107 # } 108 #} 109 110 111 # HTTPS server 112 # 113 #server { 114 # listen 443 ssl; 115 # server_name localhost; 116 117 # ssl_certificate cert.pem; 118 # ssl_certificate_key cert.key; 119 120 # ssl_session_cache shared:SSL:1m; 121 # ssl_session_timeout 5m; 122 123 # ssl_ciphers HIGH:!aNULL:!MD5; 124 # ssl_prefer_server_ciphers on; 125 126 # location / { 127 # root html; 128 # index index.html index.htm; 129 # } 130 #} 131 132 }