nginx简单配置多个php服务实例教程

后端开发   发布日期:2025年05月20日   浏览次数:184

nginx简单配置php服务(多个)

摘要:

大部分网站开发语言都要运行在服务器,比如主流的nginx、apache等等,部署服务器环境对于大部分人来说是比较陌生和复杂的,其实搞懂了之后是很简单易用的。今天就记录下部署php+nginx。

系统:mac、linux

1、安装好php和nginx程序,并运行

2、找到nginx.conf文件,默认在/etc/nginx目录下,如果找不到用一下命令查询

  1. sudo find / -name nginx.conf

3、修改nginx.conf文件

默认的nginx.conf配置

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

把server下的这段#号去掉并修改即可,将 PHP 脚本传递给在 127.0.0.1:9000 上侦听的 FastCGI 服务器

  1. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  2. #
  3. location ~ \.php$ {
  4. fastcgi_pass 127.0.0.1:9000;
  5. fastcgi_index index.php;
  6. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  7. include fastcgi_params;
  8. }

访问 localhost

参数参考:

  1. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#脚本文件请求的路径
  2. fastcgi_param QUERY_STRING $query_string; #请求的参数;如?app=123
  3. fastcgi_param REQUEST_METHOD $request_method; #请求的动作(GET,POST)
  4. fastcgi_param CONTENT_TYPE $content_type; #请求头中的Content-Type字段
  5. fastcgi_param CONTENT_LENGTH $content_length; #请求头中的Content-length字段。
  6. fastcgi_param SCRIPT_NAME $fastcgi_script_name; #脚本名称
  7. fastcgi_param REQUEST_URI $request_uri; #请求的地址不带参数
  8. fastcgi_param DOCUMENT_URI $document_uri; #与$uri相同。
  9. fastcgi_param DOCUMENT_ROOT $document_root; #网站的根目录。在server配置中root指令中指定的值
  10. fastcgi_param SERVER_PROTOCOL $server_protocol; #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
  11. fastcgi_param GATEWAY_INTERFACE CGI/1.1;#cgi 版本
  12. fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;#nginx 版本号,可修改、隐藏
  13. fastcgi_param REMOTE_ADDR $remote_addr; #客户端IP
  14. fastcgi_param REMOTE_PORT $remote_port; #客户端端口
  15. fastcgi_param SERVER_ADDR $server_addr; #服务器IP地址
  16. fastcgi_param SERVER_PORT $server_port; #服务器端口
  17. fastcgi_param SERVER_NAME $server_name; #服务器名,域名在server配置中指定的server_name

配置多个服务:

nginx.conf文件有一行

  1. include servers/*;

代表会读取servers文件夹下的所有配置文件,没有可以自己加上,并创建文件夹,servers文件夹下创建一个站点配置文件site1.conf。

  1. server {
  2. listen 80;#端口
  3. server_name site1.com;#你的站点域名/ip
  4. root /data/site1/public; #你的站点目录,绝对路径即可
  5. index index.php index.html index.htm;
  6.  
  7. #charset koi8-r;
  8. #access_log logs/host.access.log main;
  9. location / {
  10. try_files $uri $uri/ /index.php?$query_string;
  11. }
  12. error_page 500 502 503 504 /50x.html;
  13. location = /50x.html {
  14. root html;
  15. }
  16. location ~ \.php$ {
  17. fastcgi_pass 127.0.0.1:9000;
  18. fastcgi_index index.php;
  19. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  20. include fastcgi_params;
  21. }
  22. }

总结

原文地址:https://blog.csdn.net/qq_24468953/article/details/124119845

以上就是nginx简单配置多个php服务实例教程的详细内容,更多关于nginx简单配置多个php服务实例教程的资料请关注九品源码其它相关文章!